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

时间:2018-04-25 14:18:23

标签: ios swift firebase push-notification firebase-cloud-messaging

enter image description here 1。在FCM控制台中创建了一个项目来发送通知 2.该项目有一个Android应用程序,确实收到通知
3.接下来,我将iOS App添加到该项目中。我上传了证书p12文件,并发出了通知

FCM控制台显示通知已成功发送,但未在真实设备上收到。

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

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

1 个答案:

答案 0 :(得分:0)

在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