React-native-firebase: onMessage not called when notification recieved [iOS]

Created on 30 Apr 2017  路  21Comments  路  Source: invertase/react-native-firebase

I have the following code in the constructor of my root view:

this.firebase.messaging().onMessage((message) => { console.log("Got Message:"); console.log(message); Alert.alert("Got Message:", JSON.stringify(message)); });

Unfortunately, even though I get the notifications, there is no log or alert when I swipe to open the notification (on iOS) from the lock screen while the app is running in the background. I did log in getInitialNotification as well and get nothing. Am I missing anything?

Update: I tested getInitialNotification and it does fire correctly when the app is closed. However, still nothing while the app is in the foreground or background

Most helpful comment

Hi @chrisbianca, @SteffeyDev,
I am using react-native-firebase v2.0.5, target iOS version is 9.0 and react-native version is 0.47.0

My app is crashing every time I get message with error "There is no completion handler with completionhandlerId:"
This is happening on both onMessage and getInitialNotification.
I followed above steps and edited imports of RNFirebaseMessaging.h

The issue seems to be from RNFirebaseMessaging.m line 406.
I have set callbacks for getInitialNotification and onMessage also. I am not sure what I am missing.
Can you help me out with this?

All 21 comments

Ok so I was able to get it working properly by referencing react-native-fcm and forwarding the calls to react-native-firebase from my app delegate. If anyone else if having this issue, put this into your app delegate:

At the top:
#import "../../node_modules/react-native-firebase/ios/RNFirebase/RNFirebaseMessaging.h"

In didFinishLaunchingWithOptions:

[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];


  if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  } else {
    // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
    | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
    }];

    // For iOS 10 data message (sent via FCM)
    [FIRMessaging messaging].remoteMessageDelegate = self;
#endif
  }

  [[UIApplication sharedApplication] registerForRemoteNotifications];

Just before @end:

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
 {
     [RNFirebaseMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
   }

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
 {
     [RNFirebaseMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
   }

 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
     [RNFirebaseMessaging didReceiveLocalNotification:notification];
   }

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
     [RNFirebaseMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
   }

cc @chrisbianca - this seem normal to you? Thought it was all working

@SteffeyDev are you building against iOS 9? or iOS 10?

I'm having the same issue as described above:

  • getInitialNotification is fired correctly when the app is running in the background
  • onMessage doesn't get fired

I haven't tried what @SteffeyDev said he did (also I'm not sure understand if/how you referenced react-native-fcm)

I'm deploying on the last version of iOS

I've updated the documentation for iOS with all the setup that you should need for FCM: http://invertase.io/react-native-firebase/#/installation-ios?id=_3-cloud-messaging-optional

Thanks for the documentation update. Any chance you could also update the android version?

@mondoul The Android documentation already contained instructions on how to configure FCM: http://invertase.io/react-native-firebase/#/installation-android?id=_3-cloud-messaging-optional

In RNFirebaseMessaging I had to update the imports to be:

#import <React/RCTEventEmitter.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>

I had to update the imports as well to compile, and thanks for updating the documentation, I believe that was what was missing. @mondoul I referenced the documentation of react-native-fcm to know what methods to pass through form my app delegate, as it seems like the developer of this repository borrowed some of the fcm code from them.

@mondoul We need to either create a pull request to fix the imports or see if the @chrisbianca can update it as it is a small change. After that I believe this issue can be closed.

@chrisbianca I am building for iOS 8-10, and I borrowed that block of code to put in didFinishLaunchingWithOptions from https://firebase.google.com/docs/cloud-messaging/ios/client.

This has all been re-written, simplified and tested on the v2 branch

Hi @chrisbianca, @SteffeyDev,
I am using react-native-firebase v2.0.5, target iOS version is 9.0 and react-native version is 0.47.0

My app is crashing every time I get message with error "There is no completion handler with completionhandlerId:"
This is happening on both onMessage and getInitialNotification.
I followed above steps and edited imports of RNFirebaseMessaging.h

The issue seems to be from RNFirebaseMessaging.m line 406.
I have set callbacks for getInitialNotification and onMessage also. I am not sure what I am missing.
Can you help me out with this?

@kssujithcj I have same issue: There is no completion handler with completionhandlerId.

It seems it has to do with subscribing to onMessage on multiple places. If I only subscribe for messages on one place, the error doesn't pop out.

Did you find any other workaround, @kssujithcj?

@wodCZ,
No luck yet. I have paused firebase implementation.

@kssujithcj have you found a solution to the error There is no completion handler with completionhandlerId.
I'm getting the same error

Thanks

You have to call unsubscribe() which is returned from the onMessage(). If you call unsubscribe then you must run the firebase.messaging().onMessage() again to listen for notifications. I check if unsubscribe has been set, if it has, then onMessage is subscribed or listening to something already, and want to unsubscribe to prepare it for the next notification.

`if(unsubscribe !== ""){
unsubscribe()
}

        unsubscribe = firebase.messaging().onMessage((notif)=>{
        console.log(notif)
    })

        firebase.messaging().createLocalNotification({
        title: 'Welcome back',     
        body: 'Have you taken a break today?',
        icon:"icon",
        priority: 'high',
        click_action: 'ACTION',
        show_in_foreground: true,
        sound: 'default',
        opened_from_tray: true
     })
}`

@wodCZ good call on debugging, had the same issue! +1

That was it!

Example code:

let onMessageSubscription;


    // only subscribe for messages on one place to fix "no completion handler" error is iOS
    if (onMessageSubscription == null) {
      onMessageSubscription = firebase.messaging().onMessage(msg => {
        // prevent infite look
        if (!msg.local_notification) {
          console.log(msg);
          firebase.messaging().createLocalNotification({
            title: msg.title,
            body: msg.body,
            local_notification: true,
            priority: 'high', // show the notification expanded whtn
            show_in_foreground: true,
          });
        } else {
          if (msg.opened_from_tray) {
            navigate(msg);
          }
        }
      });
    }

@chrisbianca Please update this links that is 404 now

http://invertase.io/react-native-firebase/#/installation-ios?id=_3-cloud-messaging-optional
http://invertase.io/react-native-firebase/#/installation-android?id=_3-cloud-messaging-optional

@SteffeyDev Hi is your issue resolved, Cuz am suffering the same problem

facing same issue.

"react-native": "0.55.4",
"react-native-firebase": "^4.3.7",

Was this page helpful?
0 / 5 - 0 ratings