iOS 具有多个目标的通知服务扩展从不调用扩展方法
iOS Notification Service Extension with multiple targets never calls extension methods
我目前正在尝试使用 Notification Service Extension
作为在 push notification payload
中未获得有效徽章计数的应用程序的解决方法。为了解决这个问题,我想我会创建一个通知服务扩展来跟踪已收到的消息数量,并相应地跟踪计数。上周我开始工作了,现在我继续工作,但它不再工作了,我不知道为什么会这样。
正在根据目标更改 Notification Service Extension
包 ID。对于我拥有的 2 个目标,这个预构建脚本看起来像这样
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
。我试过使用我的推送通知负载,但这并没有改变结果。
有谁知道为什么会这样或者我哪里出错了。
您的通知负载肯定是问题的一部分。
在你的问题中,有效载荷是:
{
"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
键之外。
如果有 alert
、button
或 sound
键,则 "content-available"
不应出现 - 通知不能同时静音 和 面向用户。 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"
}
}
试试看扩展是否启动。
我目前正在尝试使用 Notification Service Extension
作为在 push notification payload
中未获得有效徽章计数的应用程序的解决方法。为了解决这个问题,我想我会创建一个通知服务扩展来跟踪已收到的消息数量,并相应地跟踪计数。上周我开始工作了,现在我继续工作,但它不再工作了,我不知道为什么会这样。
正在根据目标更改 Notification Service Extension
包 ID。对于我拥有的 2 个目标,这个预构建脚本看起来像这样
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
。我试过使用我的推送通知负载,但这并没有改变结果。
有谁知道为什么会这样或者我哪里出错了。
您的通知负载肯定是问题的一部分。 在你的问题中,有效载荷是:
{
"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
键之外。
如果有 alert
、button
或 sound
键,则 "content-available"
不应出现 - 通知不能同时静音 和 面向用户。 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"
}
}
试试看扩展是否启动。