5

we are using react native firebase version 6 messaging to handle push notification. From the remote notification content, we are not able to increment/ decrement the badge count on ios. So we are using push notification ios to set badge count.

It is working very well when app is in the foreground and background. But when app is closed, we are able to receive the notification, but not able to update badge count.

messaging().onMessage(async remoteMessage => {
      Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
});

messaging().setBackgroundMessageHandler(async remoteMessage => {
      PushNotificationIOS.getApplicationIconBadgeNumber(number => {
        PushNotificationIOS.setApplicationIconBadgeNumber(number + 1);
      });
      asyncstorage.saveItem("remoteMessage", "only one killed message received");
    });

here setBackgroundMessageHandler is working when the app is in the background state. As per the documentation, this method should invoke even when the app is closed. But it is not. For more clarification, I have done an async storage operation inside this method when the app has killed. But it is not working as expected.

I am adding the server side data also which I have sent for notification from the backend,

{
"to":".................",
"notification":{
 "title":"Working Good",
 "body":"backgroudn noti",
 "badge":"1",
 "sound":"./app/assets/sound/alert.wav"
},
"priority":"high",
"badge":"1",
"content_available":true
}

I would like to mention some points with these keys,

There is a badge inside the notification object, which is setting to the icon every time even the app is closed. But not able to increment/ decrement the badge count. It is always 1.

After adding "content_available" property, I am able to get update the badge count. This means after adding content_available only, I got the trigger inside setBackgroundMessageHandler method and I updated the badge count using push notification ios property.

Now I am not getting any trigger inside setBackgroundMessageHandler method when app is closed.

Is there any other keys I need to add to get the trigger inside setBackgroundMessageHandler method, even the app is closed?

I planned to try with some background libraries like react-native background fetch. But after receiving notification, without getting any trigger in the code, I can't trigger the background fetch library also to wake up the application.

So how can I get into this method, while receiving notification when app closed?

Please ping me if any more details are needed to clarify the issue. Thanks for the any help.

Dhevendhiran M
  • 994
  • 2
  • 12
  • 29

1 Answers1

-1

messaging().onMessage works only when your app is in background or foreground mode.

Use this if you want to receive message when app is closed:

messaging()
    .getInitialNotification()
    .then(remoteMessage => { console.log(remoteMessage) }
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68