iOS推送通知操作未显示

10

我的应用与Firebase服务器连接,也可以发送推送通知。现在,我想更进一步,在通知中添加一个操作。经过查阅了很多教程后,仍然无法正常工作。正如您在此处所见,操作按钮没有显示出来:

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()
        }
    }
}
根据我在这篇教程中所看到的,我也向发送的推送通知中添加了“category”键和值“NEW_SESSION”,但它也没有起作用。enter image description here 更新: 我注意到“category”键通过通知传递,所以只需要正确地处理它。userInfo字典如下:
{ "aps" : { 
"alert" : { 
"body" : "This notification has no action", 
"title" : "Test", 
} 
}, 
"category" : "NEW_SESSION" 
} 

没有,什么都没变:( 谢谢你的回答:) - Devhess
1
收到通知时,它看起来是一样的。要查看操作,您必须点击拖动通知。您做到了吗? - mfaani
我看到了这张图片。不确定我是否表达清楚。在看到您屏幕上的通知后:您是否点击并向下拖动了通知?! - mfaani
2
@Honey,弹出的警报允许/不允许已经出现了。但是,没有任何变化:( 在firebase文档中,我注意到您必须使用“click_action”而不是“category”。但这并没有改变什么,这就是为什么我现在联系firebase支持的原因。感谢您的支持:) - Devhess
1
请通过 API 调用实现可操作通知,参见这里这里 - mfaani
显示剩余10条评论
2个回答

8

这些按钮不会自动显示。在支持3D Touch的设备上,您需要对通知进行3D Touch以显示内容或按钮。在不支持3D Touch的设备上,您可以尝试向下或向左/右滑动,以显示按钮。

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,
             },
}

1
在我上面提供的教程中,有效载荷看起来像这样:{ "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”中,而我的则没有。我相信错误/bug是由此引起的。 - Devhess
@Devhess 请按照我更新的答案匹配有效载荷格式,或者您可以参考https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html。 - Nikhlesh Bagdiya
@NikhleshBagdiya 我无法更改有效载荷中的顺序。我现在已联系 Firebase 支持。 - Devhess
2
@Devhess 如果你想要实现可操作的 Firebase 通知,你可以参考这个链接:https://dev59.com/xFkT5IYBdhLWcg3wD7Vb - Nikhlesh Bagdiya
按钮不会自动显示,除非用户在通知上进行3D触摸,这是我缺失的重要信息!感谢您提到它。 - bmt22033

2
对于我来说,只有在“aps”字典中包含“category”时它才有效,而当我将其放在“aps”之外时,它就无效了。另外,你们注意到在Devhess发布的更新问题的有效负载中,“category”字段拼写错误了。因此,在那里,我们有“catogary”而不是“category”。这也可能是问题所在。 对我来说,这个有效负载是有效的:
{ "aps":{ "alert":{ "body":"This notification has ACTIONS", "title":"ACTIONABLE Notification title" }, "badge":1, "sound":"default", "category":"NEW_SESSION" } }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接