如何根据传入远程通知负载中定义的类别添加不同的操作?斯威夫特更新

2023-12-29

我正在我的两个相关应用程序中实现推送通知,到目前为止我能够发送通知、设备到设备以及主题。收到通知后,通知会显示随有效负载发送的 url 处的图像。我的目标是向主题通知添加操作,并且每个主题的操作都不同。 Ej。行动为"shop-promotions"主题通知(即"buy")将不同于“新闻”主题通知(即"play")。因此,我想在远程通知的有效负载中发送一个“类别”参数,并使用它来区分不同的传入通知并显示正确的操作集。是否可以在NotificationExtension.swift中定义操作didReceive或者还必须使用自定义 UI 来显示操作? 在https://www.pluralsight.com/guides/creating-ios-rich-push-notifications https://www.pluralsight.com/guides/creating-ios-rich-push-notifications显示为可能的,但我没有成功。

我将它们声明为:

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
            ]
        ]

UPDATES:

  1. 更改了类别声明,将它们放入我调用的静态函数中didFinishLaunchingWithOptions检查通知权限后didRegisterForRemoteNotificationsWithDeviceToken获得令牌后。
  2. Set NotificationService's info.plist没有任何UNNotificationExtensionCategory我看到那是在NotificationContent's info.plist.
  3. 尝试过不同的Target Membership两者的设置NotificationService.swift and 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
        }
    }

终于找到了所有更改后操作未显示的原因。在字典里"category": "someValue"需要声明为"click_action":"someValue"..当它转到Firebase时..不确定我是否同意这个选择..也许这只是为了给开发者原本无聊的生活增添趣味..谢谢Firebase.. 除了讽刺之外,也非常感谢您在这方面的帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何根据传入远程通知负载中定义的类别添加不同的操作?斯威夫特更新 的相关文章

  • Swift:如何让游戏中的角色只有落地后才能跳跃?

    我正在开发一款游戏 我的角色可以从一个陆地跳到另一个陆地 我已经把所有事情都做好了 除了我剩下的问题是 如果你继续点击屏幕 他可以永远跳跃 我希望他必须先落地才能再次跳跃 import SpriteKit import GameplayKi
  • iPhone:每日本地通知

    我正在尝试实现本地通知 这就是我所设置的 Current date NSDate date NSDate date Add one minute to the current time NSDate dateToFire date date
  • iOS 确定视频中的帧数

    如果我有一个 Swift 中的 MPMoviePlayerController MPMoviePlayerController mp MPMoviePlayerController contentURL url 有没有办法获取视频中的帧数u
  • iOS 何时清理本地应用程序 ./tmp 目录?

    iOS什么时候清理本地应用程序 tmp目录 请注意 这不是一个骗局这个问题 https stackoverflow com questions 3593900 iphone storage in tmp directory 我问的是应用程序
  • iOS 上关键 ClientState 警告的默认访问速度缓慢

    在测试我的 iOS 应用程序时 我收到 对关键 ClientState 的默认访问速度慢 耗时 0 034635 秒 容差为 0 020000 警告 它似乎是间歇性发生的 我试图环顾四周看看它是关于什么的 但我并不完全确定 任何帮助表示赞赏
  • iPhone 的翻译器?

    我对为 iPhone 制作一个解释器很感兴趣 这将是一个实验性的想法 但可能会很棒 我喜欢让我自 己的语言适合移动计算和数学的想法 我查阅了很多资料 发现有关 iPhone 上的口译员的信息很复杂 苹果会允许什么 我见过这个应用程序 这是一
  • 在 Swift 中的 For 循环中更改对象的属性

    我创建了一个名为 ShoppingList 的简单结构 struct ShoppingList var shoppingListId NSNumber var title String var groceryItems GroceryIte
  • Apple 由于崩溃而拒绝了我的应用程序,无法重现它

    我刚刚上传了一个应用程序到应用程序商店 它是为ios 7开发的 他们拒绝了该应用程序 因为我无法重现崩溃 他们向我发送了这份崩溃报告 Exception Type EXC BAD ACCESS SIGSEGV Exception Subty
  • Swift 中计算只读属性与函数

    在 Swift WWDC 简介会话中 只读属性description被证明 class Vehicle var numberOfWheels 0 var description String return numberOfWheels wh
  • 无法在 xcode 8 beta 6 上编译 AWS CustomIdentityProvider

    我在 iOS 应用程序中使用 Amazon Cognito 和 Facebook 登录 直到 beta 5 为止此代码从这个SO线程 https stackoverflow com questions 37597388 aws cognit
  • 从字典创建 Swift 对象

    如何根据 Swift 字典中的查找值动态实例化类型 希望这对其他人有用 我们需要进行一些研究才能弄清楚这一点 目标是避免巨大的 if 或 switch 语句从值创建每个对象类型的反模式 class NamedItem CustomStrin
  • Grand Central Dispatch (GCD) 调度源标志

    我最近不再使用 to GCD 调度来源 https developer apple com documentation dispatch 1385630 dispatch source create监视文件更改 效果很好 API 也变得更加
  • iOS 7 NS 单线程安全合并冲突

    重新排序两行后 在单线程应用程序上保存简单的数据时遇到问题 我已经成功地简化了编码以重现错误 并且希望其他人尝试这一点时得到第二个意见 这是一次健全性检查 因为我怀疑 iOS 7 引入的核心数据问题 而这在 iOS 6 中工作正常 首先 启
  • SDK 和 iOS 部署目标。

    我使用最新的 SDK 4 1 构建项目并设置 iOS 部署目标 3 0 如果我使用4 0 sdk的某些方法 我可以在真正的iPhone 3 0上运行我的项目吗 您只能在 OS 3 设备上运行您的项目 如果有条件地编码围绕您想要使用的 OS
  • UIScrollView 与 UITabBarController 切断

    我有一个 UIScrollView 我将其放置在视图中 界面生成器文档 xib m h 但是 UIScrollView 的下半部分被剪切 并且由于我有一个 UITabBarController 而没有显示其下半部分 我在 appdelega
  • iOS 有 INTERNET 权限吗?

    我在 iOS 设备上的 flutter dio 包上遇到了一个奇怪的问题 我编写了一个向 url 发送 GET 请求的应用程序 Android 上一切正常 但 iOS 上的请求似乎无法通过 没有发生任何错误 什么也没有 我在 Android
  • 使用 PDFOutline 将 TOC 添加到 Swift/Cocoa 中的 PDFDocument

    我正在开发一个小程序 将多个单页 PDF 合并到一个多页 PDF 中 我正在 Swift4 MacOS Cocoa 中工作 但我一生都无法在 Swift 中找到任何类型的示例来创建大纲 仅遍历现有的大纲 我对此非常熟悉 使用对文档的最佳猜测
  • 如何制作像 Facebook 应用程序一样的登录屏幕?

    如何制作像 Facebook 应用程序一样带有 电子邮件 和 密码 文本字段的登录屏幕 Facebook登入 http extdesenv com wp content uploads 2012 05 facebook login ios
  • iOS 目标 c 中的 AES/CBC/PKCS5Padding 结果与 Android 不同

    我在 Android 应用程序中使用 AES CBC PKCS5Padding 代码就像 private static String TRANSFORMATION AES CBC PKCS5Padding private static St
  • 从应用程序内发送电子邮件中的图像和文本

    如何从我的应用程序内通过电子邮件发送图像和文本 表格数据形式 请大家帮忙并提出建议 谢谢 void sendMailWithImage UIImage image if MFMailComposeViewController canSend

随机推荐