Ios 如何根据传入的远程通知负载中定义的类别添加不同的操作?迅速更新

Ios 如何根据传入的远程通知负载中定义的类别添加不同的操作?迅速更新,ios,swift,push-notification,Ios,Swift,Push Notification,我正在我的两个相关应用程序中实现推送通知,到目前为止,我能够发送通知、设备到设备和主题。在接收到它时,通知将在url处显示与有效负载一起发送的图像。我的目标是向主题通知中添加操作,每个主题的操作都会有所不同。Ej。“店铺促销”主题通知(即“购买”)的操作不同于“新闻”主题通知(即“播放”)。因此,我想在远程通知的有效负载中发送一个“category”参数,并使用它来区分不同的传入通知,并显示正确的操作集。是否可以在NotificationExtension.swiftdidReceive中定义操

我正在我的两个相关应用程序中实现推送通知,到目前为止,我能够发送通知、设备到设备和主题。在接收到它时,通知将在url处显示与有效负载一起发送的图像。我的目标是向主题通知中添加操作,每个主题的操作都会有所不同。Ej。
“店铺促销”
主题通知(即
“购买”
)的操作不同于“新闻”主题通知(即
“播放”
)。因此,我想在远程通知的有效负载中发送一个“category”参数,并使用它来区分不同的传入通知,并显示正确的操作集。是否可以在NotificationExtension.swift
didReceive
中定义操作,或者必须使用自定义UI来显示操作? 在这方面,我尽可能做到了,但我没有成功

我宣布它们为:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

        print("NotificationService: dide receive called")
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)


        if let bestAttemptContent = bestAttemptContent {
            if let urlString = bestAttemptContent.userInfo["attachment-url"] as? String,
                let data = NSData(contentsOf: URL(string:urlString)!) as Data? {
                let path = NSTemporaryDirectory() + "attachment"
                _ = FileManager.default.createFile(atPath: path, contents: data, attributes: nil)

                do {
                    let file = URL(fileURLWithPath: path)
                    let attachment = try UNNotificationAttachment(identifier: "attachment", url: file,options:[UNNotificationAttachmentOptionsTypeHintKey : "public.jpeg"])
                    bestAttemptContent.attachments = [attachment]

                } catch {
                    print(error)

                }

                // Actions
                if let category = bestAttemptContent.userInfo["category"] as? String {

                    if category == "shop-promotions" {
                        let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [])
                        let close = UNNotificationAction(identifier: "close", title: "Close", options: [])

                        let category = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
                        UNUserNotificationCenter.current().setNotificationCategories([category])
                    }
                }

            } // else {
            //                if let contentHandler: ((UNNotificationContent) -> Void) =
            //                    self.notificationContentHandler,
            //                    let content: UNNotificationContent = self.notificationContent {
            //                    contentHandler(content)  }
    

            let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [])
            let close = UNNotificationAction(identifier: "close", title: "Close", options: [])

            let category = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
            UNUserNotificationCenter.current().setNotificationCategories([category])


            contentHandler(bestAttemptContent)
        }
这是为通知发送的词典:

let postParams: [String : Any] = [
            "to": topic,
            "notification": [
                //                    "badge" : 1, sendig the badge number, will cause aglitch
                "body": body,
                "title": title,
                "subtitle": subtitle,
                "text": "some text",
                "sound" : true, // or specify audio name to play
                "priority": "high",
                "content_available": true,
                "mutable_content": true,
                "category": "shop-promotions"
            ],
            "data" : [
                "attachment-url": dataUrl,
//                "media_type":"image",
                "productId": productId,
                "price": price
            ]
        ]
更新:

  • 更改了类别声明,将它们放入一个静态函数中,我在检查通知权限后调用该函数,并在获取令牌后调用该函数
  • 设置
    NotificationService
    info.plist
    而不设置任何
    UNNotificationExtensionCategory
    ,正如我在
    NotificationContent
    info.plist
    中看到的那样
  • 已尝试对NotificationService.swift和info.plist进行不同的
    目标成员身份设置。正确的配置是什么
  • 仍然在相同的情况下,只显示带有通知的图像

    你能看到没有正确设置的地方吗

    操作功能如下所示:

    static func configurePushCategories() {
            if #available(iOS 10.0, *) {
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted:Bool, error:Error?) in
                    if error != nil {
                        print(error?.localizedDescription as Any)
                    }
                    if granted {
                        print("Permission granted")
                    } else {
                        print("Permission not granted")
                    }
                }
    
                //  actions
    
                let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [.foreground])
                let play = UNNotificationAction(identifier: "play", title: "Play", options: [.foreground])
                let close = UNNotificationAction(identifier: "close", title: "Close", options: [.foreground])
    
    //            let shopPromotionsCategory = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
    //            let fixItPromotionsCategory = UNNotificationCategory(identifier: "fix-it-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
    //            let cityFixItNewsCategory  = UNNotificationCategory(identifier: "city-fix-it-news", actions: [play,close], intentIdentifiers: ["play","close"], options: [])
    //            let countryFixItNewsCategory = UNNotificationCategory(identifier: "country-fix-it-news", actions: [play,close], intentIdentifiers: ["play","close"], options: [])
    
                let shopPromotionsCategory = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
    
                let fixItPromotionsCategory = UNNotificationCategory(identifier: "fix-it-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
    
                let cityFixItNewsCategory  = UNNotificationCategory(identifier: "city-fix-it-news", actions: [play,close], intentIdentifiers: [], options: [])
    
                let countryFixItNewsCategory = UNNotificationCategory(identifier: "country-fix-it-news", actions: [play,close], intentIdentifiers: [], options: [])
    
                UNUserNotificationCenter.current().setNotificationCategories([shopPromotionsCategory,fixItPromotionsCategory, cityFixItNewsCategory, countryFixItNewsCategory])
            } else {
                // Fallback on earlier versions
            }
        }
    


    当推送服务器想要向用户发送通知时,它可以 将具有适当值的类别键添加到通知的 有效载荷。当iOS看到带有类别键的推送通知时,它 查找应用程序注册的类别。如果iOS发现 如果匹配,则会显示与通知对应的操作

    #通知有效负载:

     {
        "aps": {
            "category": "MEETING_INVITATION",
            "alert": {
                "title": "Weekly Staff Meeting",
                "body": "Every Tuesday at 2 pm"
            }
        },
        "MEETING_ID": "123456789",
        "USER_ID": "ABCD1234"
    }
    
    要支持可操作的通知,您必须:

    • 在启动时从iOS应用程序声明一个或多个通知类别

    • 为通知类别分配适当的操作

    • 处理您注册的所有操作

    • 生成通知时,为通知有效负载分配类别标识符


    最后找到了在所有更改之后操作未显示的原因。在字典
    “category”中:“someValue”
    需要声明为
    “click\u action”:“someValue”
    。当它转到Firebase时。。我不确定我是否同意这个选择。也许这只是为了给作为开发人员本来会很无聊的生活增添趣味。谢谢你Firebase。。
    除了讽刺,我也非常感谢您在这方面的帮助。

    您需要在DidFinishLaunching中注册通知类别,并在AppDelegate中针对您的类别使用选项。在didFinishLaunchingWithOptions中移动您的代码
    let buy=UNNotificationAction(标识符:“buy”,标题:“buy”,选项:[])let close=UNNotificationAction(标识符:“close”,标题:“close”,选项:[])let category=UNNotificationCategory(标识符:“店铺促销”,操作:[购买,关闭],意向标识符:[],选项:[])UNUserNotificationCenter.current().setNotificationCategories([category])
    当推送服务器想要向用户发送通知时,它可以向通知的有效负载添加具有适当值的类别键。当iOS看到带有类别键的推送通知时,它会查找应用程序注册的类别。如果iOS找到匹配项,它会显示相应的操作和通知n、 @Milan我确实按照你们的建议做了更改,请看更新问题,但我仍然处于相同的状态。你能看到它出了什么问题吗?@iMHiteshSurani我执行了更改,现在设置为您在回答中列举的四个步骤,但没有任何更改。哦,我明白了。如果使用自定义用户界面,我会偏离这一点通知内容扩展您将不得不在didReceive中声明响应(uu响应NotificationViewController,所以我认为动作必须在NotificationService.swift中声明,因为我遵循的教程在那里声明动作,这让我走错了方向..所以我可以避免在
    info.plist
    中设置类别词典,对吗?非常感谢。这是因为Firebase的有效负载用于所有受支持平台的指令通知,“类别”是苹果特定的键。要为苹果定制Firebase的消息,您需要包括以下内容:
    “APN”:{“payload”:{“category”:“someValue”}
    payload对象是苹果的有效负载,您可以在其中放置“aps”、“category”和其他内容。否则,您可以使用“click\u action”在顶层,以便Firebase将其正确转换为所有平台中的相关键(“点击动作”实际上是Android版本的“类别”)