2

當我發送帶有推送負載的mutable-content:1時沒有顯示通知,它既沒有顯示可變內容推送,也沒有顯示Notification服務擴展中的斷點,Notification內容擴展也是工作正常。我沒有修改Notification服務擴展中的代碼,它是由xcode生成的默認代碼。我是否在創建通知服務擴展時錯過了某些內容,或者可能是設備設置的問題。我前幾天在同一臺設備上測試過,通知服務擴展工作正常。通知服務擴展不起作用

下面是我的服務擴展代碼

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) 
     } 
    } 
} 

編輯1:下面是我推的有效載荷

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

EDIT1:這個問題似乎特定設備上運行的10.2.1,我檢查了10.2上運行的其他設備,它觸發了通知服務擴展。

+0

你如何發送包含「mutable-content:1」的Json?如果它與FCM通過「_」更改「 - 」 – SeikoTheWiz

+0

Hi @SeikoTheWiz i我沒有使用FCM – princerk

+0

Hi @princerk我也在嘗試實現通知服務擴展,但它並不修改我的內容,即使我使用的是默認的蘋果代碼 我是否需要爲應用標識啓用推送通知我爲通知服務擴展? 請幫助我完成工作所需的步驟。 –

回答

9

最後我終於解決了這個問題。我不確定是什麼導致了這個問題,但經過一些實驗後,我意識到,不僅我的服務擴展,而且所有其他已安裝的應用程序的服務擴展都無法正常工作,並且在發送推送有效負載的時間爲「mutable-content:1」時,Notification未顯示。在重新啓動設備後,它開始工作正常。我正在使用iPhone 6s。

+0

謝謝。適用於我。奇怪的錯誤。 –