我的应用程序连接到 Firebase 服务器,也可以发送推送通知。现在,我想更进一步,为通知添加一个 Action 。在抛出大量教程之后,它仍然不适合我。操作按钮未显示,如您在此处所见:
这是我的代码:
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”的“类别”键,但效果不佳。
更新: 我注意到“类别”键是通过通知传递的,因此正确处理它只是一个问题。 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/