首页 > 解决方案 > 在 Swift 4 中将 UNNotificationAction 添加到 Firebase FCM 推送通知

问题描述

我正在尝试在 FCM 推送通知时添加一些自定义操作。我在注册通知后尝试添加这些。

UNUserNotificationCenter.current().delegate = self
        let acceptAction = UNNotificationAction(identifier: "ACCEPT_ACTION",
                                                title: "Accept",
                                                options: UNNotificationActionOptions(rawValue: 0))
        let declineAction = UNNotificationAction(identifier: "DECLINE_ACTION",
                                                 title: "Decline",
                                                 options: UNNotificationActionOptions(rawValue: 0))
        let center = UNUserNotificationCenter.current()
        if #available(iOS 11.0, *) {
            let meetingInviteCategory = UNNotificationCategory(identifier: "MEETING_INVITATION",
                                                               actions: [acceptAction, declineAction],
                                                               intentIdentifiers: [],
                                                               hiddenPreviewsBodyPlaceholder: "",
                                                               options: .customDismissAction)
            center.setNotificationCategories([meetingInviteCategory])
        }
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]


        center.requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })

但不幸的是,该动作仍未出现。有没有办法做到这一点?这就是我想要达到的目标。

例子

我的 firebase FCM 设置和所有通知工作正常。我只是无法为这些通知添加操作。

标签: iosswiftfirebase-cloud-messaging

解决方案


我知道了。我只需要在 fcm/send 的正文中添加 click_action 字段。并将 click_action 键设置为类别标识符。


推荐阅读