【发布时间】:2023-04-04 02:54:01
【问题描述】:
我尝试在我的应用上实现 Firebase 云消息传递。
我使用以下代码注册通知
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
当应用程序在后台时一切正常,但当应用程序在前台时,根本不调用 didReceiveRemoteNotification。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
print("please help")
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(.newData)
}
有人知道这可能会出错吗?谢谢!
编辑:当应用程序处于前台时我不需要看到通知显示,我只想处理来自通知的消息
【问题讨论】:
-
这个完成处理程序根本没有调用吗?
-
声明 UNUserNotificationCenterDelegate 与否?
标签: ios swift firebase firebase-cloud-messaging