FCM正在使用前景和后台条件下的ios 10设备正确接收,但是对于iphone 8(ios 11.2),FCM正在后台模式下接收。 Appdelegate包含
@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) {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[messageKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print("fcm-->\(userInfo)")
// Change this to your preferred presentation option
if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing.
completionHandler([])
}
NotificationCenter.default.post(name: Notification.Name(rawValue: messageKey), object: nil,userInfo: userInfo)
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[messageKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print("fcm-->1\(userInfo)")
NotificationCenter.default.post(name: Notification.Name(rawValue: messageKey), object: nil,userInfo: userInfo)
completionHandler()
}
}
// [END ios_10_message_handling]
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
updateGCMIdInServer(fcmToken)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message 10 +: \(remoteMessage)")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: messageKey), object: nil, userInfo: remoteMessage.appData)
}
}
但未达到通知观察员功能。帮助我将数据从appdelegate传递到特定的viewcontroller
答案 0 :(得分:0)
可能对你有帮助 -
@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) {
let userInfo = notification.request.content.userInfo
//From here we will navigate to other screens:-
print("User Info : \(userInfo)")
// Change this to your preferred presentation option
//completionHandler([])
completionHandler([.alert,.sound])
}