【发布时间】:2017-12-16 01:23:46
【问题描述】:
我的应用连接到 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()
}
}
}
正如我在this 教程中看到的,我还在我发送的推送通知中添加了值为“NEW_SESSION”的“类别”键,但它不能正常工作。
更新: 我注意到“类别”键是通过通知传递的,所以它只是一个正确处理它的问题。 userInfo 字典如下所示:
{ "aps" : {
"alert" : {
"body" : "This notification has no action",
"title" : "Test",
}
},
"category" : "NEW_SESSION"
}
【问题讨论】:
-
我不确定。从您的观点行动:尝试将 从
[.foreground]更改为[] -
不,没有任何改变:(谢谢你的回答,伙计:)
-
仅供参考,当您收到通知时,它看起来是一样的。要查看操作,您必须点击并向下拖动通知。你这样做了吗?
-
@Honey,弹出允许/不允许的警报。不,什么都没有改变:(在 [firebase doc] (firebase.google.com/docs/cloud-messaging/…) 我注意到你必须使用“click_action”而不是“category”。但这并没有改变任何东西,这就是我联系的原因火力基地现在支持。感谢您的支持:)
标签: ios swift firebase push-notification apple-push-notifications