My app has a connection to the Firebase-server, also to send Push Notifications. Now, I want to go a step further and add an action to the notifications. After going throw lots of tutorials, it´s still not working for me. The action-button is not showing up, as you can see here:
我的应用程序与Firebase服务器连接,也用于发送推送通知。现在,我想更进一步,为通知添加一个动作。在抛出大量教程之后,它仍然不适合我。操作按钮未显示,您可以在此处看到:
Here is my code:
这是我的代码:
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()
}
}
}
As I saw at this tutorial, I also added the "category" key with the value "NEW_SESSION" to the push notification I´m sending, but it´s not working as well.
正如我在本教程中看到的那样,我还在我发送的推送通知中添加了值“NEW_SESSION”的“category”键,但它不能正常工作。
Update: I noticed that the "category" key is passed through the notification, so its just a question to handle it right. The userInfo Dictionary looks like this:
更新:我注意到“类别”键是通过通知传递的,所以它只是一个问题来处理它。 userInfo字典如下所示:
{ "aps" : {
"alert" : {
"body" : "This notification has no action",
"title" : "Test",
}
},
"catogary" : "NEW_SESSION"
}
2 个解决方案
#1
4
The buttons do not appear on their own. On supported devices you have to 3D touch the notifications to show the content or buttons. On non-supported devices you can try swiping down or left/right for the buttons to show.
按钮不会单独出现。在支持的设备上,您必须通过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()
}
And add UNUserNotificationCenterDelegate
methods to handle action.
并添加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.
}
}
}
And don't forgot to set delegate as:
并且不要忘记将委托设置为:
UNUserNotificationCenter.current().delegate = self
Payload format
{
"aps" : {
"category" : "NEW_MESSAGE_CATEGORY"
"alert" : {
"body" : "Acme message received from Johnny Appleseed",
"title" : "Test",
},
"badge" : 3,
},
}
#2
1
For me it works when I have the "category" included in "aps" dictionary and it does not work when I have it outside of "aps". And also, did you guys noticed that in the payload that Devhess posted as an update to his question the "category" field is misspelled. So instead of "category" we have "catogary" there. This also could be the problem. This payload works for me:
对我来说,当我将“类别”包含在“aps”字典中时它起作用,当我在“aps”之外使用它时它不起作用。而且,你们有没有注意到在Devhess发布的有效载荷中,作为对他的问题的更新,“类别”字段拼写错误。因此,不是“类别”,而是“catogary”。这也可能是问题所在。这个有效载荷对我有用:
{ "aps":{ "alert":{ "body":"This notification has ACTIONS", "title":"ACTIONABLE Notification title" }, "badge":1, "sound":"default", "category":"NEW_SESSION" } }
{“aps”:{“alert”:{“body”:“此通知有操作”,“标题”:“可操作的通知标题”},“徽章”:1,“声音”:“默认”,“类别” :“NEW_SESSION”}}
#1
4
The buttons do not appear on their own. On supported devices you have to 3D touch the notifications to show the content or buttons. On non-supported devices you can try swiping down or left/right for the buttons to show.
按钮不会单独出现。在支持的设备上,您必须通过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()
}
And add UNUserNotificationCenterDelegate
methods to handle action.
并添加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.
}
}
}
And don't forgot to set delegate as:
并且不要忘记将委托设置为:
UNUserNotificationCenter.current().delegate = self
Payload format
{
"aps" : {
"category" : "NEW_MESSAGE_CATEGORY"
"alert" : {
"body" : "Acme message received from Johnny Appleseed",
"title" : "Test",
},
"badge" : 3,
},
}
#2
1
For me it works when I have the "category" included in "aps" dictionary and it does not work when I have it outside of "aps". And also, did you guys noticed that in the payload that Devhess posted as an update to his question the "category" field is misspelled. So instead of "category" we have "catogary" there. This also could be the problem. This payload works for me:
对我来说,当我将“类别”包含在“aps”字典中时它起作用,当我在“aps”之外使用它时它不起作用。而且,你们有没有注意到在Devhess发布的有效载荷中,作为对他的问题的更新,“类别”字段拼写错误。因此,不是“类别”,而是“catogary”。这也可能是问题所在。这个有效载荷对我有用:
{ "aps":{ "alert":{ "body":"This notification has ACTIONS", "title":"ACTIONABLE Notification title" }, "badge":1, "sound":"default", "category":"NEW_SESSION" } }
{“aps”:{“alert”:{“body”:“此通知有操作”,“标题”:“可操作的通知标题”},“徽章”:1,“声音”:“默认”,“类别” :“NEW_SESSION”}}