具有多个目标的iOS Notification Service Extension永远不会调用扩展方法

时间:2018-09-18 14:25:03

标签: ios swift apple-push-notifications notificationservices

我目前正尝试使用Notification Service Extension作为无法在push notification payload中获得有效徽章计数的应用的解决方法。为了解决这个问题,我认为我将创建一个Notification Service Extension,以跟踪已收到的邮件数量,并相应地跟踪计数。上周我得到了这项工作,现在我继续进行下去,它不再起作用了,我也不知道为什么这样做。

Notification Service Extension捆绑包ID正在根据目标进行更改。对于我拥有的两个目标,此预构建脚本如下所示:

buildID=${PRODUCT_BUNDLE_IDENTIFIER}
extId="AppPushNotification"

/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $buildID.$extId" "${SRCROOT}/${extId}/Info.plist"

我在扩展程序中使用的代码

import UIKit
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...
            let userDefaults = UserDefaults.init(suiteName: "group.com.my.bundle.notification-service-extension")
            let unreadMessages = userDefaults?.integer(forKey: "unreadMessages")
            let newUnreadMessages = unreadMessages != nil ? unreadMessages! + 1 : 0

            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
            bestAttemptContent.badge = NSNumber(value: newUnreadMessages)
            let userInfo = bestAttemptContent.userInfo
            guard let info = userInfo["aps"] as? [AnyHashable: Any],
                let data = info["data"] as? [AnyHashable: Any],
                let chatId = data["chatId"] as? String else { return }

            let unreadMessagesInChat = userDefaults?.integer(forKey: chatId)
            let newUnreadMessagesInChat = unreadMessagesInChat != nil ? unreadMessagesInChat! + 1 : 0

            userDefaults?.set(newUnreadMessages, forKey: "unreadMessages")
            userDefaults?.set(newUnreadMessagesInChat, forKey: chatId)


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

}

我正在使用Pusher推送测试通知负载。我正在发送的有效载荷:

{
   "aps":{
      "mutalbe-content":1,
      "content-available":1,
      "sound":"true",
      "alert":{
         "title":"Vera Pauw",
         "body":"test bericht"
      },
      "data":{
         "type":"test",
         "message":"test bericht",
         "chatId":"3b002aa6-1117-4206-b54f-8bc2bd689f1c",
         "fromId":"",
         "fromname":"Vera Pauw"
      }
   }
}

我已成功收到通知,但从未触发扩展代码。我已经尝试了多种调试方式,例如在运行应用程序后选择调试目标,或者从扩展目标运行应用程序,但是Notification Service的过程将始终显示Waiting to Attach。我尝试使用推送通知有效负载,但这并没有改变结果。

有人知道为什么会这样或我要去哪里吗

1 个答案:

答案 0 :(得分:1)

您的通知有效负载绝对是问题的一部分。 在您的问题中,有效载荷是:

{
   "aps":{
      "mutalbe-content":1,
      "content-available":1,
      "sound":"true",
      "alert":{
         "title":"Vera Pauw",
         "body":"test bericht"
      },
      "data":{
         "type":"test",
         "message":"test bericht",
         "chatId":"3b002aa6-1117-4206-b54f-8bc2bd689f1c",
         "fromId":"",
         "fromname":"Vera Pauw"
      }
   }
}

aps键只能包含Apple的键。您的data字典应位于aps键之外。

如果有"content-available"alertbutton键,则sound不应该出现-通知不能同时是静默面向用户。 content-available仅用于静默通知。

"mutalbe-content"键也拼错了,这就是为什么您的分机没有被激活的原因!

我看到很多人都遇到同样的问题,所以我写了a simple push notification validation tool you can use to troubleshoot these problems

取决于您要完成正确的有效负载的方式,可能看起来像这样:

{
   "aps":{
      "mutable-content":1,
      "alert":{
         "title":"Vera Pauw",
         "body":"test bericht"
      }
   },
  "data":{
     "type":"test",
     "message":"test bericht",
     "chatId":"3b002aa6-1117-4206-b54f-8bc2bd689f1c",
     "fromId":"",
     "fromname":"Vera Pauw"
  }
}

尝试一下,看看扩展程序是否启动。