Notificaton Service Extension Not working

﹥>﹥吖頭↗ 提交于 2020-06-14 06:53:44

问题


Notification is not being displayed when I send mutable-content:1 with push payload neither it hits the breakpoint inside the Notification service extension, although without mutable-content push is being displayed, also Notification content extension is working fine. I did not modified the code inside Notification service extension its the default generated by the Xcode. Am I missing something while creating notification service extension or it might be the issue with the device setting. I have tested on the same device few days ago and notification service extension was working fine.

Below is my code for service extension

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

           contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

edit 1: below is my push payload

{  
   "aps":{  
      "sound":"default",
      "mutable-content": 1,
      "category": "myCategory",
      "alert":{  
         "body":"this is a custom push",
         "subtitle":"subtitle of the push",
         "title":"Push Test"
      }

   }
}

edit1: The problem seems on that particular device running on 10.2.1, I checked with other device running on 10.2, it was triggering the Notification service extension.


回答1:


Finally I was able to resolve the issue. I am not sure what was causing the issue but after some experiments I realised that not only my service extension but all other installed app's service extension was not working and when sending the push payload with "mutable-content: 1, Notification was not displayed. After rebooting the device it started working fine. I was using iPhone 6s.



来源:https://stackoverflow.com/questions/42695564/notificaton-service-extension-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!