我的应用与Firebase服务器连接,也可以发送推送通知。现在,我想更进一步,在通知中添加一个操作。经过查阅了很多教程后,仍然无法正常工作。正如您在此处所见,操作按钮没有显示出来:
这是我的代码:
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](https://istack.dev59.com/Sca26.webp)
{ "aps" : {
"alert" : {
"body" : "This notification has no action",
"title" : "Test",
}
},
"category" : "NEW_SESSION"
}