简体   繁体   中英

FCM iOS project not receiving Notification - FCM console shows success but device does not receive

在此处输入图片说明 1. Created a project in FCM console to send notifications.
2. The project has an Android app that does receive the notification
3. Next, I added the iOS App into that project. I uploaded the certificate p12 file, and made a notification

The FCM console shows that the notification was sent successfully, but it is not received on the real device.

Push Notification enabled in App IS. Checked it in Capabilities New cer file created from App - Created the Development cer file, Downloaded and double click and added into Keychain access. In that Private key exported and created the p12 file. created the Provision profile and download and double click and add into xcode. App run from xCode and got the FCM Token - Using the Token I send Notification and also send notification based on the App. But still i didn't receive any notification. But in my Notification Page it shows success.

any idea to get to fix this issue. am using xCode 9.2 and App in Swift 4 language.

Do something like this, in AppDelegate, out of its body. as its extension.

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()
    }
}

Inside 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)
                }
            }
        }
    }

Also add in your project plist .

FirebaseAppDelegateProxyEnabled = NO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM