【问题标题】:iOS Push Notification Action is not showing upiOS 推送通知操作未显示
【发布时间】:2017-12-16 01:23:46
【问题描述】:

我的应用连接到 Firebase 服务器,也可以发送推送通知。现在,我想更进一步,在通知中添加一个操作。在扔了很多教程之后,它仍然不适合我。操作按钮未显示,您可以在此处看到:

这是我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.applicationIconBadgeNumber = 0
    FirebaseApp.configure()
    registerForPushNotifications()
    return true
}

func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        print("Permission granted: \(granted)")

        guard granted else { return }

        let viewAction = UNNotificationAction(identifier: "addToCal",
                                              title: "Zum Kalender hinzufügen",
                                              options: [.foreground])

        let newsCategory = UNNotificationCategory(identifier: "NEW_SESSION",
                                                  actions: [viewAction],
                                                  intentIdentifiers: [],
                                                  options: [])
        UNUserNotificationCenter.current().setNotificationCategories([newsCategory])
        self.getNotificationSettings()
    }
}

func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

正如我在this 教程中看到的,我还在我发送的推送通知中添加了值为“NEW_SESSION”的“类别”键,但它不能正常工作。

更新: 我注意到“类别”键是通过通知传递的,所以它只是一个正确处理它的问题。 userInfo 字典如下所示:

{ "aps" : { 
"alert" : { 
"body" : "This notification has no action", 
"title" : "Test", 
} 
}, 
"category" : "NEW_SESSION" 
} 

【问题讨论】:

  • 我不确定。从您的观点行动:尝试将 [.foreground] 更改为 []
  • 不,没有任何改变:(谢谢你的回答,伙计:)
  • 仅供参考,当您收到通知时,它看起来是一样的。要查看操作,您必须点击并向下拖动通知。你这样做了吗?
  • @Honey,弹出允许/不允许的警报。不,什么都没有改变:(在 [firebase doc] (firebase.google.com/docs/cloud-messaging/…) 我注意到你必须使用“click_action”而不是“category”。但这并没有改变任何东西,这就是我联系的原因火力基地现在支持。感谢您的支持:)
  • herehere。您必须通过 API 调用来完成。

标签: ios swift firebase push-notification apple-push-notifications


【解决方案1】:

按钮不会单独出现。在受支持的设备上,您必须 3D 触摸通知才能显示内容或按钮。在不受支持的设备上,您可以尝试向下或向左/向右滑动以显示按钮。

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("Permission granted: \(granted)")

            guard granted else { return }

            let action = UNNotificationAction(identifier: "addToCal", title: "Zum Kalender hinzufügen", options: [])
            let category = UNNotificationCategory(identifier: "NEW_SESSION", actions: [action], intentIdentifiers: [], options: [])
            UNUserNotificationCenter.current().setNotificationCategories([category])

            self.getNotificationSettings()
        }

并添加UNUserNotificationCenterDelegate 方法来处理操作。

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // Print message ID.
        // Print full message.
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.actionIdentifier == "addToCal" {

            //Handel action.
        }
    }
}

别忘了将委托设置为:

UNUserNotificationCenter.current().delegate = self

有效载荷格式

{
    "aps" : {
              "category" : "NEW_MESSAGE_CATEGORY",
               "alert" : {
                        "body" : "Acme message received from Johnny Appleseed",
                        "title" : "Test",
                    },
               "badge" : 3,
             },
}

【讨论】:

  • 在上面的教程 i 链接中,有效负载如下所示:{ "aps": { "alert": "Breaking News!", "sound": "default", "link_url": "https://raywenderlich.com", "category": "NEWS_CATEGORY" } },我的是 { "aps" : { "alert" : { "body" : "This notification has no action", "title" : "Test", } }, "catogary" : "NEW_SESSION" }。如您所见,在工作负载中,“类别”被放置在“aps”中,而我的不是。我很确定错误/错误是由此引起的。
  • @Devhess 请匹配有效载荷格式作为我更新的答案,或者您可以关注developer.apple.com/library/content/documentation/…
  • @NikhleshBagdiya 我无法更改有效负载中的顺序。我现在已经联系了 Firebase 支持。
  • @Devhess 获取可操作的 Firebase 通知,您可以关注 stackoverflow.com/questions/39137727/…
  • 除非用户对通知执行 3D 触摸,否则按钮不会自动显示,这是我遗漏的一条重要信息!感谢您提及。
【解决方案2】:

对我来说,当我将“类别”包含在“aps”字典中时它可以工作,而当我将它放在“aps”之外时它不起作用。另外,你们有没有注意到,在 Devhess 作为对他的问题的更新发布的有效负载中,“类别”字段拼写错误。因此,我们在那里有“类别”而不是“类别”。这也可能是问题所在。 这个有效载荷对我有用:

{ "aps":{ "alert":{ "body":"This notification has ACTIONS", "title":"ACTIONABLE Notification title" }, "badge":1, "sound":"default", "category":"NEW_SESSION" } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多