FCM推送通知不适用于iOS 11 [英] FCM Push notifications do not work on iOS 11

查看:262
本文介绍了FCM推送通知不适用于iOS 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase作为后端。我也使用FCM作为Firebase提供的功能之一。 FCM在iOS 10中运行良好,但在切换到iOS 11之后,推送通知停止发送到用户设备,而我自己也没有收到Firebase控制台中的云功能或通知部分发送的任何推送通知。如何解决这个问题?



更新:我从Firebase Notifcations发送了几个推送通知,但是他们没有发送。 b
$ b

  // MARK: - 推送通知
$ b $扩展名AppDelegate:UNUserNotificationCenterDelegate {

func registerPushNotification _ application:UIApplication){
//对于iOS 10显示通知(通过APNS发送)
UNUserNotificationCenter.current()。delegate = self
$ b $ authOptions:UNAuthorizationOptions = [。 (
options:authOptions,
completionHandler:{_,_in})

/ b
UNUserNotificationCenter.current().requestAuthorization对于iOS 10数据消息(通过FCM发送)
Messaging.messaging()。委托=自


func应用程序(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {
//何时这个代码的通知运行良好,还没有。 b
$ func application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:任何]){
//如果您的应用程序在后台收到通知消息,
//只有当用户点击启动应用程序的通知时,才会触发此回调。
// TODO:处理通知数据
//打印消息ID。
如果让messageID = userInfo [gcmMessageIDKey] {
debugPrint(Message ID:\(messageID))
}

//打印完整的信息。 (UIBackgroundFetchResult)
debugPrint(userInfo) - > Void){
//如果在应用程序处于后台时收到通知消息,
//直到用户点击启动应用程序的通知,才会触发此回调。
// TODO:处理通知数据
//打印消息ID。
如果让messageID = userInfo [gcmMessageIDKey] {
print(Message ID:\(messageID))
}

//打印完整信息。
debugPrint(userInfo)
$ b $ completionHandler(.newData)
}

//显示推送通知

func userNotificationCenter( UNUserNotificationCenter,didReceive响应:UNNotificationResponse,withCompletionHandler completionHandler:@escaping() - > Void){
如果让userInfo = response.notification.request.content.userInfo as? [String:Any] {
让routerManager = RouterManager()
routerManager.launchRouting(userInfo)
}
completionHandler()
}

func userNotificationCenter(_ center:UNUserNotificationCenter,willPresent notification:UNNotification,withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions) - > Void){
if userInfo = notification.request.content.userInfo as? [String:Any] {
如果让categoryID = userInfo [categoryID] as? String {
if categoryID == RouterManager.Categories.newMessage.id {
if let currentConversation = ChatGeneralManager.shared.currentChatPersonalConversation,let dataID = userInfo [dataID] as? String {
// dataID是newMessage对话的id
如果currentConversation.id == dataID {
completionHandler([])
return
}
}
}
}
如果让badge = notification.request.content.badge {
AppBadgesManager.shared.pushNotificationHandler(userInfo,pushNotificationBadgeNumber:badge.intValue)
}

completionHandler([。alert,.sound,.badge])
}

}

// [START ios_10_data_message_handling]
extension AppDelegate:MessagingDelegate {

func messaging(_ messaging:Messaging,didRefreshRegistrationToken fcmToken:String){
let pushNotificationManager = PushNotificationManager()
pushNotificationManager.saveNotificationTokenInDatabase(token:fcmToken,成功:无,失败:无)
}


//当iOS应用处于前台时,在iOS 10设备上接收数据消息。
func application(received remoteMessage:MessagingRemoteMessage){
debugPrint(remoteMessage.appData)
}

}
pre>

解决方案

FirebaseInstanceID 2.0.3推送通知似乎不起作用。它帮助我设置:FirebaseInstanceID,2.0.0。
也许在下一个版本中这个问题将会被解决。


I use Firebase as a backend. I also use FCM as one of the provided features from Firebase. FCM worked well in iOS 10, but after switching to iOS 11, push notifications stopped coming to user devices, and I myself did not receive any push notifications sent from the cloud functions or the Notification section in the Firebase Console. How to fix this problem?

Update: I sent several push notifications from Firebase Notifcations, but they do not come.

// MARK: - Push notification

extension AppDelegate: UNUserNotificationCenterDelegate {

    func registerPushNotification(_ application: UIApplication) {
        // 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 })

        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //When the notifications of this code worked well, there was not yet.
        Messaging.messaging().apnsToken = deviceToken
    }

    // [START receive_message]
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            debugPrint("Message ID: \(messageID)")
        }

        // Print full message.
        debugPrint(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        debugPrint(userInfo)

        completionHandler(.newData)
    }

    // showing push notification

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if let userInfo = response.notification.request.content.userInfo as? [String : Any] {
            let routerManager = RouterManager()
            routerManager.launchRouting(userInfo)
        }
        completionHandler()
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if let userInfo = notification.request.content.userInfo as? [String : Any] {
            if let categoryID = userInfo["categoryID"] as? String {
                if categoryID == RouterManager.Categories.newMessage.id {
                    if let currentConversation = ChatGeneralManager.shared.currentChatPersonalConversation, let dataID = userInfo["dataID"] as? String  {
                        // dataID is conversationd id for newMessage
                        if currentConversation.id == dataID {
                            completionHandler([])
                            return
                        }
                    }
                }
            }
            if let badge = notification.request.content.badge {
                AppBadgesManager.shared.pushNotificationHandler(userInfo, pushNotificationBadgeNumber: badge.intValue)
            }
        }
        completionHandler([.alert,.sound, .badge])
    }

}

// [START ios_10_data_message_handling]
extension AppDelegate : MessagingDelegate {

    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        let pushNotificationManager = PushNotificationManager()
        pushNotificationManager.saveNotificationTokenInDatabase(token: fcmToken, success: nil, fail: nil)
    }


    // Receive data message on iOS 10 devices while app is in the foreground.
    func application(received remoteMessage: MessagingRemoteMessage) {
        debugPrint(remoteMessage.appData)
    }

}

解决方案

It appears that with FirebaseInstanceID 2.0.3 push notifications don't work. It helped me to set: pod 'FirebaseInstanceID', "2.0.0". Maybe in next version this will be fixed.

这篇关于FCM推送通知不适用于iOS 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆