FCM iOS 项目未收到通知 - FCM 控制台显示成功但设备未收到

Posted

技术标签:

【中文标题】FCM iOS 项目未收到通知 - FCM 控制台显示成功但设备未收到【英文标题】:FCM iOS project not receiving Notification - FCM console shows success but device does not receive 【发布时间】:2018-04-25 14:18:22 【问题描述】:

1。在 FCM 控制台中创建了一个项目以发送通知。 2. 该项目有一个接收通知的 android 应用 3. 接下来,我将 ios App 添加到该项目中。我上传了证书p12文件,并做了通知

FCM控制台显示通知发送成功,但真机收不到。

在应用 IS 中启用了推送通知。 在功能中检查了它 从 App 创建的新 cer 文件 - 创建开发 cer 文件,下载并双击并添加到钥匙串访问中。在该私钥中导出并创建了 p12 文件。 创建Provision Profile并下载并双击并添加到xcode中。 应用程序从 xCode 运行并获得 FCM 令牌 - 使用令牌我发送通知并基于应用程序发送通知。但我仍然没有收到任何通知。 但在我的通知页面中,它显示成功。

解决此问题的任何想法。 我正在使用 Swift 4 语言的 xCode 9.2 和 App。

【问题讨论】:

您有证书问题。你能告诉我你是如何从 Keychain 获取 P12 文件的以及你是如何制作它的吗? 您是否注册了远程通知并添加了从 Firebase 下载的 GoogleService-Info.plist 文件? 当您在客户端生成令牌时,请尝试按照docs 中的说明从控制台发送到此令牌,我发现这有助于解决问题。这就是我发现我的 App ID Prefix 写错的原因。 @RatulSharker 是的,我已经注册了远程通知。并添加了“GoogleService-Info.plist” @VeerSuthar 我已经从钥匙串导出 Apple Development Push services:MyappID 的私钥下载 p12 【参考方案1】:

在 AppDelegate 中,在它的主体之外做这样的事情。作为它的扩展。

extension AppDelegate : MessagingDelegate 
    func application(received remoteMessage: MessagingRemoteMessage) 
        print("remoteMessage: MessagingRemoteMessage")
    



extension AppDelegate : UNUserNotificationCenterDelegate 
    // MARK: - UNUserNotificationCenterDelegate
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler:
        @escaping (UNNotificationPresentationOptions) -> Void) 
        // Always show the incoming notification, even if the app is in foreground
        print("Received notification in foreground:")
        //let jsonString = notification.request.content.userInfo.jsonString ?? ""
        //print("\(jsonString)")
        completionHandler([.alert, .badge, .sound])
    

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 
        print("Received notification response")
        //let jsonString = response.notification.request.content.userInfo.jsonString ?? ""
        //print("\(jsonString)")
        userInfo = response.notification.request.content.userInfo
        NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
        completionHandler()
    

在 AppDelegate 中

 // MARK: - Push Notification Code
    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

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)

        print(userInfo)

        if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.active 
            if application.applicationState == UIApplicationState.background 
                NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
            
            else if application.applicationState == UIApplicationState.inactive 
                NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
            
            else if application.applicationState == UIApplicationState.active 
                    NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
            
        

        completionHandler(UIBackgroundFetchResult.newData)
    

    // MARK: -  Delegates Method For Push Notifications
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) 
        if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.active 
            //opened from a push notification when the app was on background

            if application.applicationState == UIApplicationState.background 
                NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
            
            else if application.applicationState == UIApplicationState.inactive 
                NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
            else if application.applicationState == UIApplicationState.active 
                if self.isAlreadyInConsult == false 
                    NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
                
            
        
    

同时添加您的项目plist

FirebaseAppDelegateProxyEnabled = NO

【讨论】:

以上是关于FCM iOS 项目未收到通知 - FCM 控制台显示成功但设备未收到的主要内容,如果未能解决你的问题,请参考以下文章