简体   繁体   English

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

[英]iOS Notification Service Extension with multiple targets never calls extension methods

I'm currently trying to use a Notification Service Extension as a workaround for an app that doesn't obtain a valid badge count in the push notification payload . 我目前正在尝试将Notification Service Extension用作无法在push notification payload获得有效徽章计数的应用的解决方法。 To fix this I figured I would create a Notification Service Extension that keeps track of the amount of messages that have been received and that will keep track of the count accordingly. 为了解决这个问题,我认为我将创建一个Notification Service Extension,以跟踪已收到的邮件数量,并相应地跟踪计数。 I got this working last week now I continued on it and it doesn't work at all anymore and I'm clueless as to why this is. 上周我开始进行这项工作,现在我继续进行下去,现在它不再工作了,我也不知道为什么这样做。

The Notification Service Extension bundle id is being changed according to the target. Notification Service Extension捆绑包ID正在根据目标进行更改。 This pre-build script looks like this for the 2 targets I have 对于我拥有的两个目标,此预构建脚本如下所示

buildID=${PRODUCT_BUNDLE_IDENTIFIER}
extId="AppPushNotification"

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

The code I'm using in my extension 我在扩展程序中使用的代码

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

}

I'm pushing my test notification payload by using Pusher . 我正在使用Pusher测试通知负载。 The payload I'm sending: 我发送的有效负载:

{
   "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"
      }
   }
}

I get the notification successfully but it doesn't trigger the extensions code ever. 我成功获得了通知,但从未触发扩展代码。 I've tried debugging in multiple ways, like selecting the debug target after running the app, or running the app from the extension target but the process of the Notification Service will always say Waiting to Attach . 我已经尝试了多种调试方法,例如在运行应用程序后选择调试目标,或者从扩展目标中运行应用程序,但是Notification Service的过程将始终显示Waiting to Attach I've tried playing around with my push notification payload but this didn't change the result. 我尝试使用推式通知有效负载,但这并没有改变结果。

Does anyone have any idea why this is happening or where I'm going wrong. 有谁知道为什么会这样或我哪里出错了。

Your notification payload is definitely part of the problem. 您的通知有效负载绝对是问题的一部分。 In your question the payload is: 在您的问题中,有效载荷是:

{
   "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"
      }
   }
}

The aps key should only contain Apple's keys. aps密钥只能包含Apple的密钥。 Your data dictionary should be outside the aps key. 您的data字典应该在aps键之外。

The "content-available" should not be present if the alert , button , or sound keys are there - a notification can not be both silent and user-facing. 如果存在alertbuttonsound键,则"content-available"不应该存在-通知不能同时是静默的面向用户的。 content-available is only for silent notifications. content-available仅适用于静默通知。

The "mutalbe-content" key is also misspelled, which is why your extension is not being activated! "mutalbe-content"键也拼错了,这就是为什么您的扩展名未被激活的原因!

I see many people having the same issues so I wrote a simple push notification validation tool you can use to troubleshoot these problems . 我看到很多人遇到相同的问题,因此我编写了一个简单的推送通知验证工具,可以用来解决这些问题

Depending on what you are trying to accomplish a corrected payload might look like this: 根据您要完成的有效载荷而定,看起来可能像这样:

{
   "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"
  }
}

Try that and see if the extension launches. 尝试一下,看看扩展是否启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM