【问题标题】:UNNotificationAction not showing in remote notificationUNNotificationAction 未显示在远程通知中
【发布时间】:2017-11-06 23:35:33
【问题描述】:

我正在实现可操作的远程通知,但notification 来时操作按钮未显示。

有效载荷

{"aps":{"alert":"测试中.. (11)","badge":1,"sound":"default"},"category":"newnote"}

我的代码

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        self.configureUserNotifications()
        self.registerForRemoteNotification()

        return true
    }


func registerForRemoteNotification() {
    let center  = UNUserNotificationCenter.current()
    center.delegate = self
    center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
    if error == nil{
        UIApplication.shared.registerForRemoteNotifications()
    }
}


func configureUserNotifications() {
        let acceptAction = UNNotificationAction(identifier:
            "accept", title: "✅ Accept note", options: [])
        let rejectAction = UNNotificationAction(identifier:
            "reject", title: "❌ Reject note", options: [])
        let category =
            UNNotificationCategory(identifier: "newnote",
                                   actions: [acceptAction,rejectAction],
                                   intentIdentifiers: [],
                                   options: [])

        UNUserNotificationCenter.current()
            .setNotificationCategories([category])
    }



    func application(_ application: UIApplication,
                     didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Registration for remote notifications failed")
        print(error.localizedDescription)
    }

    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Registered with device token: \(deviceToken.hexString)")
        self.deviceToken = deviceToken.hexString
    }


    //Called when a notification is delivered to a foreground app.

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("User Info = ",notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }

    //Called to let your app know which action was selected by the user for a given notification.

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("User Info = ",response.notification.request.content.userInfo)
        print("Response received for \(response.actionIdentifier)")

        if response.actionIdentifier == "accept" {

        }
        else if response.actionIdentifier == "reject" {

        }
        completionHandler()
    }

【问题讨论】:

  • 您是否在整个应用程序中调用一次setNotificationCategories‍‍‍?无论何时调用它,它都会替换以前的类别。因此,在注册后,您应该准备好所有类别
  • 这不适用于上面的代码示例,也许这会对某人有所帮助 - 确保您只调用一次 UNUserNotificationCenter.current().setNotificationCategories() 并将所有类别放在一个数组中。如果您使用不同的类别多次调用它,一切似乎都可以正常工作,但似乎从未呈现自定义操作。上面的评论帮助我解决了我的问题。

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


【解决方案1】:

我认为如果您将UNUserNotificationCenter.current() .setNotificationCategories([category]) 移动到您的registerForRemoteNotification 函数中,问题就会解决。像这样的:

 if #available(iOS 8.0, *) {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: Set([category]))
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多