firebase / quickstart-ios

Firebase Quickstart Samples for iOS
https://firebase.google.com
Apache License 2.0
2.8k stars 1.47k forks source link

FCM Push notifications do not work on iOS 11 #327

Closed alexanderkhitev closed 7 years ago

alexanderkhitev commented 7 years ago

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

}
lsalvoni commented 7 years ago

@dhorrock You need to subscribe to the FIRMessagingDelegate and add the missing delegate method; see sample code in the AppDelegate.m of the MessagingExample project for your language of choice.

raghuyt commented 7 years ago

Hi all am also facing same problem ,but am not installed plugin coco pod on ios ,installed only "cordova-plugin-fcm" and done FCM setting its working all IOS Expect 11. am able to get token id also, sending notification through API getting successful responses also ,but notification not coming IOS 11 but 10 its coming

Any suggestions?

kerimsari commented 7 years ago

Can not get it to work either same problem with @o15a3d4l11s2.

ericklarac commented 7 years ago

@kerimsari @raghuyt @dhorrock What I did was:

  1. I opened terminal on the folder of the project, deleted the Podfile.lock.

  2. Then exec pod install, with the following pods on Podfile:

    • pod 'Firebase/Core'
    • pod 'Firebase/Messaging'
    • pod 'FirebaseInstanceID', "2.0.0"
  3. Then I reviewed the certificates.

  4. Run the project.

Note: If you are facing problems and you don't find a solution, I recommend you to start a new project and review each step. You can use the quickstart-ios example of firebase or this example which I found it awesome.

raghuyt commented 7 years ago

Hi @ericklarac

How can i install this for cordova apps

pod 'Firebase/Core' pod 'Firebase/Messaging' pod 'FirebaseInstanceID', "2.0.0"

Please can you help me with this .

kerimsari commented 7 years ago

Hi @ericklarac

I've done what you said and i can send notifications via topic target but not to user segment..

Please suggest

ericklarac commented 7 years ago

@raghuyt Open up Terminal on the folder of the project then edit the Podfile, you can use nano or vi and just paste those lines (i.e. mine looks like this):

image

Then you save it and run the command: pod install, open the .xcworkspace with Xcode, run the project and you are done.

@kerimsari If the notifications status on the firebase consoles is completed, click the notification and look the chart if sent is 0 it's because firebase isn't storing the tokens. Actually, I don't know how user segment works on the firebase console at all, but hope I could work.

raghuyt commented 7 years ago

@ericklarac thank you now its working in all IOS versions ,till now IOS version 11.0.2 its working all devices what i made changes is this updated our cordova to new version and platform IOS to new version

Dragna commented 7 years ago

I had the same problem on my phone and solved it by restarting the phone, I was receiving the push after that. I hope it Will be the same for you.

ordinaryman09 commented 7 years ago

Firebase released a new update, anybody tested the new one?

shihochan commented 7 years ago

I have a problem don't receive push notification on my some devices.

Environment:

Devices: iPhone6S Plus (9.3.2) iPhone5S (10.3.2) iPad Pro (10.2.1) iPhone7 Plus (10.2.1)

In my Podfile.lock:

  - Firebase/Core (4.3.0):
  - Firebase/Messaging (4.3.0):
    - FirebaseMessaging (= 2.0.4)
  - FirebaseInstanceID (2.0.4)

Did v4.0.4~ really fix push problem??

ericklarac commented 7 years ago

Are you uploading the apple certificates (.p12 files) to firebase?

kroikie commented 7 years ago

@shihochan It may be useful to create a new issue since this one is closed. There are different ways in which messages should be handled between iOS 9 and 10 so that could be an issue but a new issue with more detail of your setup and how you are sending the message would be helpful.

charlotteliang commented 7 years ago

Agree with @kroikie Looks like this thread has been involved with many off-topic issues. If your issue is different than what's on the title (that's been closed), please file a new issue and we will look into each individual one. Thanks!