iOS 推送通知操作未显示

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

我的应用程序连接到 Firebase 服务器,也可以发送推送通知。现在,我想更进一步,为通知添加一个 Action 。在抛出大量教程之后,它仍然不适合我。操作按钮未显示,如您在此处所见:

enter image description here

这是我的代码:

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”的“类别”键,但效果不佳。 enter image description here

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

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

最佳答案

按钮不会单独出现。在支持的设备上,您必须 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.
        }
    }
}

并且不要忘记将委托(delegate)设置为:

UNUserNotificationCenter.current().delegate = self

有效载荷格式

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

关于iOS 推送通知操作未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043729/

相关文章:

ios - 为什么从库中获取照片时 imagePickerController 会崩溃,但从相机中获取照片却可以正常工作

arrays - 如何从 [NSMutableDictionary] 中的值按数字顺序从高到低排列?

android - Firebase onChildAdded 第一次为空

ios - IOS应用应该使用什么Cache方式

ios - UIPanGestureRecognizer 最大化和最小化 UIView

ios - userDefaults.setObject 在设备上崩溃但在 iOS 模拟器中没有

javascript - 在子 Firebase HTML 的子级中读取/写入数据

node.js - 错误 : Cannot find module 'grpc_node.node'

ios - 为特定 ASBD 缓冲区获取正确的数字数据类型转换

ios - 如何在 Google Drive API (Swift 5) 中禁用文件夹递归