2016-09-23 36 views
8

我試圖實現新的通知服務擴展,但我有一個問題。iOS 10不調用通知服務擴展

在我NotificationService.swift文件我有這樣的代碼:

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]" 

     print(bestAttemptContent.body) 

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

當我得到了一個推送通知的didReceive(_要求:UNNotificationRequest,withContentHandler contentHandler時:@escaping(UNNotificationContent) - >無效)方法從不叫。

也許我誤解了這個擴展是如何工作的?

+0

嗨。 你解決了這個問題嗎? – user3703910

回答

3

您的推送通知有效負載should contain the "mutable-content" : 1 key value pair

遠程通知的APS字典包括設置爲1

例推送通知有效載荷JSON的值可變的內容密鑰:

{ 
    "aps":{ 
      "alert": { 
        "body": "My Push Notification", 
        "title" : "Notification title"}, 
      "mutable-content" : 1, 
      "badge":0}, 
} 

這工作完全正常,我獲取推送通知如下: Push Notification

另請注意:

您無法修改無聲通知或只能播放聲音或標記應用程序圖標的無聲通知。

您可以嘗試PusherHouston來測試推送通知。

+0

在有效載荷中,我看到可變內容:1。我在alert對象和apns對象中嘗試了這個值,但是他們都沒有爲我工作 – just

9
  1. 而不是運行該應用程序。您必須運行服務擴展作爲目標。然後它會詢問你有哪個應用程序運行服務擴展,然後選擇你的應用程序,它會發送通知。

  2. 確保部署目標小於服務擴展中的設備操作系統。

  3. 請確保有效載荷應包含「可變內容」:1.

    {"aps" : { 
         "alert" : { 
          "title" : "Introduction To Notification", 
          "subtitle" : "Session 707", 
          "body" : "New Notification Look Amazing" 
         }, 
         "sound" : "default", 
         "category" : "message", 
         "badge" : 1, 
         "mutable-content": 1 
        }, 
        "attachment-url": "https://api.buienradar.nl/Image/1.0/RadarMapNL" 
    } 
    
  4. 不要添加「內容提供」標誌的接入點,如果需要添加使其0「。 Content-available「:0,

+1

運行Extension作爲目標是關鍵!花了我一段時間來找出這一個。感謝您指出! – kakubei

+0

2.救我吧。謝謝:) – ivarun

+0

我的通知{「registration_ids」:[「devicetoken」],「mutable_content」:true,「data」:{「post_details」:{「pcm_message」:「asdf」,「pcm_img_url」:「portalvh/15683。 jpg「;}}},」notification「:{」title「:」demo push「,」body「:」this is push body「}}但仍然不會調用Notification Service Extension,其中有問題或缺少一些信息我已經在整個項目中設置了部署目標10.0 –

7

檢查服務擴展的部署目標。

我在10.1版本的設備上測試時將部署目標設置爲10.2,直到我改變它才調用擴展。

另一個問題可能是調試..爲了使其工作您需要連接服務擴展過程。在Xcode菜單中調試>附加到進程>擴展名

0

我越來越瘋狂了。最後我意識到我的deployment targetNotification Service Extension10.3(我的手機也是)。我改爲10.2,它完美的作品