react-native-push-notification: onNotification is not calling when app is in background

Bug

When I am calling fcm server push notification api data is not showing on onNotification function.

Environment info

react-native info output:

 // paste it here

Library version: 3.4.0

Steps To Reproduce

I use the following code for recieving push notification

  1. Call the service from postman collection for fcm
  2. we got data in onNotification when app is in foreground but not in backgorund …

Describe what you expected to happen:

Reproducible sample code

PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android) onRegister: function (token) { console.log(“TOKEN:”, token); thats.setState({deviceToken:token}); },

  // (required) Called when a remote or local notification is opened or received
  onNotification: function (notification) {
    

    console.log('** NOTIFICATION: **', notification);

  if (Platform.OS === 'ios') {
    if (notification.alert.length !== 0) {
      //handleNotification(notification)
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  } else {
    //handleNotification(notification)
  }

  
  },

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

 
  requestPermissions: true,
});

PushNotification.popInitialNotification((notification) => { 
  console.log(notification); 
})

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 68 (6 by maintainers)

Commits related to this issue

Most upvoted comments

This is why onNotification is not trigger. Look at my previous message.

You can’t use notification field with background handler. You must use data (and not combined with notification). Then if you want to notify the user, trigger a local notification.

Hi,

There is no channelId defined.

Hi @romelbonnie There is an userInteraction field in the notification object. true == user has pressed the notification. Regards,

This is why onNotification is not trigger. Look at my previous message.

You can’t use notification field with background handler. You must use data (and not combined with notification). Then if you want to notify the user, trigger a local notification.

I am still facing this problem. can you give some example code? Thank you

Hi, If you want a background process in any cases, you must send data-only notification. This way you should be able to trigger a local notification and run your process. Persist your state at the end of the process. Long process are not allowed (not sure but it’s <10/20s), prefer send data in notification than requesting a server when possible.

Hi @mehul2013 On the current version of the library, scheduled notification doesn’t trigger onNotification. You can test the version on dev branch but some changes will be made before the release. I try to update the CHANGELOG.md on dev as soon as changes are made.

@taymer I understand. Do you have any tips/suggestions for me? I need to call onNotification ALWAYS when a notification is received to update a redux state to add a notification badge number on my hamburger button, so the user will know if and how many notifications he has. I was progressing well but got stuck on this. Check the badge I’m using.

@Dallas62 singleTop.

I do not remember the exact reason why though. I feel like I’ve had to mess with that a few times over the years. I wrote a particularly useless commit message for that too…

EDIT: I ended up making the following change. Not an Android developer, so I don’t know if it makes a real difference, but at least it is not broken. Then again, the original worked for me as well.

    public RNPushNotification(ReactApplicationContext reactContext) {
        super(reactContext);

        reactContext.addActivityEventListener(this);

+        // This is used to delivery callbacks to JS. Instantiated before registering activityLifeCycleCallbacks.
+        mJsDelivery = new RNPushNotificationJsDelivery(reactContext);

        Application applicationContext = (Application) reactContext.getApplicationContext();
        applicationContext.registerActivityLifecycleCallbacks(this);

        // The @ReactNative methods use this
        mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext);
-        // This is used to delivery callbacks to JS
-        mJsDelivery = new RNPushNotificationJsDelivery(reactContext);

        mRNPushNotificationHelper.checkOrCreateDefaultChannel();
    }