quickstart-ios icon indicating copy to clipboard operation
quickstart-ios copied to clipboard

FCM Push notifications do not work on iOS 11

Open alexanderkhitev opened this issue 9 years ago • 64 comments

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

alexanderkhitev avatar Sep 24 '17 18:09 alexanderkhitev

I have the same problem.

caiovidaln avatar Sep 25 '17 05:09 caiovidaln

Hi @caiovidaln ! Please see my answer on my question https://stackoverflow.com/questions/46391818/fcm-push-notifications-do-not-work-on-ios-11 It helped me, I'm currently testing, so that everything works as before, but unfortunately it will be necessary to wait until Apple confirms the new Build.

alexanderkhitev avatar Sep 25 '17 06:09 alexanderkhitev

We have the same issue. Once the app goes into the background, no pushes are received at all.

berlininsomniac avatar Sep 26 '17 12:09 berlininsomniac

Also not receiving push notifications with iOS 11 and latest firebaseMessaging 2.0.3, firebase 4.2.0. I have used the apps.sh script to test and i receive those notifications, but using the fcm id and sending through the console and I receive nothing.

I just did a drop in replace from previous version 1.2.1 and 3.11.1 (had to add one delegate function for token update).

visskiss avatar Sep 27 '17 17:09 visskiss

I noticed from your code, you're not running the request for push authorization from application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, perhaps you call it at later time?

I have similar issue. I updated to Firebase 4.2.0 and FirebaseMessaging 2.0.3, my issue is I'm not getting notification if I do request push notification at later time. (I'm doing soft request/custom dialog to check if user want to receive push notification, before using the system request). I was able to reproduce it from the quickstart project by adding a delay to the requestAuthorization for push. Here's my SO thread: https://stackoverflow.com/questions/46443189/firebase-messaging-not-sending-notification-when-push-notification-authenticatio

ordinaryman09 avatar Sep 27 '17 21:09 ordinaryman09

There is a known issue where the FCM token is not always being associated with the APNs device token if UIApplication.shared.registerForRemoteNotifications() is not called early enough. This has already been fixed in the FirebaseInstanceID SDK, and should be coming out soon.

In the meantime, you can:

  1. Lock your FirebaseInstanceID pod to 2.0.0 in your Podfile, or

  2. Ensure you're calling UIApplication.shared.registerForRemoteNotifications() on app start, ideally before FirebaseApp.configure().

For testing, you can trigger a new FCM token by uninstall/reinstall your debug app on your device.

rsattar avatar Sep 27 '17 21:09 rsattar

Got it. do you have any ETA when the version will be released? Tried to lock it, setting pod 'FirebaseInstanceID', '~> 2.0.0' , but it kept using the 2.0.3, How do I downgrade it?

ordinaryman09 avatar Sep 27 '17 21:09 ordinaryman09

@rsattar Thanks so much! Locking FirebaseInstanceID to 2.0.0 worked for me. @ordinaryman09 remove the arrow for an exact match: pod 'FirebaseInstanceID', '2.0.0'

jetboh avatar Sep 27 '17 21:09 jetboh

@ordinaryman09 to specify exact versions, you should use '= 2.0.0'. CocoaPods' ~> specifier will use the most recent compatible version.

morganchen12 avatar Sep 28 '17 01:09 morganchen12

Both downgrading the FirebaseInstanceID and FirebaseMessaging and placing the UIApplication.shared.registerForRemoteNotifications() as the first thing in the didFinishLaunchingWithOptions still doesn't work and gives a similar error. Is there anything else I can do?

vazkir avatar Sep 28 '17 18:09 vazkir

@10686142 try uninstalling the app and reinstalling with the code changes, to ensure that a new FCM token gets issued

rsattar avatar Sep 28 '17 18:09 rsattar

Yeah somehow I still can't get it to work, but maybe I did something wrong since I am new to this. But maybe someone knows what it can be: I followed this tutorial: https://www.youtube.com/watch?v=_jy_Hlskmiw&t=710s Which came down to me delegate looking like this: https://github.com/douglasdevelops/PushNotificationSample/blob/master/PushNotificationSample/AppDelegate.swift

Is he maybe missing something is his explanation or am I missing something?

Thanks in advance!

vazkir avatar Sep 28 '17 19:09 vazkir

Thanks @jtbh89 & @rsattar , I downgraded to 2.0.0 and it works for me now. @10686142 what messages are you seeing? Do you get the tokens?

richardliveraise avatar Sep 28 '17 19:09 richardliveraise

2017-09-28 21:26:20.067218+0200 App Imperium News[1026:80671] [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented 2017-09-28 21:27:05.337971+0200 App Imperium News[1026:80673] TIC Read Status [5:0x0]: 1:57 2017-09-28 21:27:05.338807+0200 App Imperium News[1026:80673] TIC Read Status [5:0x0]: 1:57

vazkir avatar Sep 28 '17 19:09 vazkir

Yeah I did also receive the token. And I got it to work earlier today that I could also see the push notification message in my debug log, but I always get that message about FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented

vazkir avatar Sep 28 '17 19:09 vazkir

I got it to work finally guys! I added this piece of code: func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken }

vazkir avatar Sep 28 '17 20:09 vazkir

Beginners mistake I think, thanks for your help! @rsattar @richardliveraise

vazkir avatar Sep 28 '17 20:09 vazkir

it works for me after downgrading to 2.0.0, thanks guys!

ordinaryman09 avatar Sep 28 '17 20:09 ordinaryman09

@rsattar Without using CocoaPods, how is it possible to downgrade to 2.0.0 to fix this? Can you provide a link to download the working framework(s)?

driverslicense avatar Sep 29 '17 00:09 driverslicense

@candywriter you can download the tar.gz from the link in the 2.0.0 podspec without using CocoaPods.

https://dl.google.com/dl/cpdc/9fe91bfb8772be4b/FirebaseInstanceID-2.0.0.tar.gz

morganchen12 avatar Sep 29 '17 00:09 morganchen12