代码之家  ›  专栏  ›  技术社区  ›  NoSixties

具有多个目标的iOS通知服务扩展从不调用扩展方法

  •  0
  • NoSixties  · 技术社区  · 8 年前

    Notification Service Extension 作为一个解决方案,应用程序没有获得一个有效的徽章计数在 push notification payload . 为了解决这个问题,我想我应该创建一个通知服务扩展来跟踪已接收的消息量,并相应地跟踪计数。我上个星期做了这个,现在我继续做它,它不再工作了,我不知道为什么会这样。

    这个 正在根据目标更改包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 回复  |  直到 8 年前
        1
  •  4
  •   quellish    8 年前

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

    {
       "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 密钥应该只包含苹果的密钥。你的 data 词典应该在词典外 钥匙。

    这个 "content-available" 如果 alert , button ,或 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"
      }
    }
    

    试试看扩展是否启动。

    推荐文章