evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

Android: Updating badge number not working when app is in background or closed state. #413

Open binzailani3136 opened 7 years ago

binzailani3136 commented 7 years ago

Hi there. I've problem with updating badge number. on iOS, appIcon badge number automatically updated everytime when I get push notification even the app is in background or closed. but on Android, appIcon badge number is only changed when app is in foreground and if the app is in background or closed state, I receive fcm push notification but the app badge icon is not automatically changed. I don't want to go on with FCM.setBadgeNumber(), because if I call it in javascript code, it will not fired when app is closed or in background and it will not work too. So is there anyway to update badge number automatically when getting push notification even the app is closed on Android? Do I need to run kind of background service or task which calls FCM.setBadgeNumber() to achieve this? iOS is really working well and I want Android should working same as iOS. My message payload looks like this. message = { to: 'push token', data: { badge: 3 }, // for Android badge number and only works when app is alive. notification: { title: 'xxx', body: 'xxx', badge: 3 // for iOS badge number and this changes iOS app badge count even app is closed. } }; Thanks

evollu commented 7 years ago

hi @binzailani3136 I'm not expert on the badge implementation and I don't have android device that supports badge to test. If you can debug the android solution, try check if BadgeHelper.java's setBadgeCount gets called correctly.

amandahla commented 7 years ago

Hi @binzailani3136 Do you find a solution for this? I'm with the same problem.

zoranmarjanovic commented 7 years ago

Same here, I have tried to debug, but it seems to me that is setBadge is not called at all when app is in background or close.

tjkang commented 7 years ago

+1 same here { "react": "16.0.0-alpha.12", "react-native": "0.45.1", "react-native-fcm": "^9.1.0", or 7.5.1 }

evollu commented 7 years ago

can you try set "content_available": true and "priority": "high"?

rogoja commented 6 years ago

solved this problem?

shekharskamble commented 6 years ago

Facing same problem...

nes123 commented 6 years ago

any solution ?

rickylaiabc commented 6 years ago

me too. can't make it work on Android

evollu commented 6 years ago

for Android, make sure you don't pass notification in payload otherwise it won't when app is in background

tawsifalam commented 6 years ago

I am facing the same issue. I used the below format to send notification but still it doesn't show the badge number on app icon when the app is in background or killed. But it does update the badge number when the app is in active state. The sound works fine when app is in background or kill state. { "to":"FCM_DEVICE_TOKEN", "data": { "custom_notification": { "body" : "Test body", "title": "Test title", "badge": 15, "sound": "default", "content_available": true, "priority":"high", "show_in_foreground": true } } }

shekharskamble commented 6 years ago

@tawsifalam - put badge number in data payload instead of custom_notification

var payload ={
                    data: {
                        custom_notification: JSON.stringify({
                        ...
                        }),
                        badge: '15',
                    }
                }
tawsifalam commented 6 years ago

@shekharskamble I did that and it works only when the app is in foreground. It doesn't update or show badge count when the app is in killed or background state.

danhnguyeen commented 6 years ago

same problem, is there any solution to fix? "react": "16.3.1", "react-native": "0.55.4", "react-native-fcm": "^16.0.0"

decodedmrq commented 5 years ago

facing same issue 😢 . I can't update badge on Android when app in background state. but IOS is auto trust notification badge, everything is ok on IOS. Any solution ?

decodedmrq commented 5 years ago

I'm resolved. Badge worked on Android when app state is background and forefround. I'm using brozot/Laravel-FCM to push notification. I'm manage badge on serve. With Android devices, i pass only Data builder, so badge is fine.

Here is my source using laravel and fcm package:

    $token = 'Your Device Token Here ';
    // Option builder
    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60 * 20);
    $option = $optionBuilder->build();
    // Data builder
    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData([
        'custom_notification' => [
            "sound" => "default",
            "priority" => "high",
            "show_in_foreground" => true,
            "opened_from_tray" => 1,
            "title" => "test title",
            "body" => "test body",
            "type" => "2" // Custom data
        ],
        'badge' => 6,
    ]);
    $data = $dataBuilder->build();

    \FCM::sendTo($token, $option, null, $data);

References: http://faysal515.xyz/2018/06/react-native-push-notifications-counter-implementation/

gitpayal commented 5 years ago

Anyone able to resolve this issue. Working fine on IOS but on Android, badge is not updated in background or closed mode. Thanks !

MinaFSedrak commented 5 years ago

Any update !?

ajinkyap7 commented 5 years ago

Hi Everyone,

This issue has been around for a while but I was wondering if anyone has found a solution. In our application, the badge count is getting updated but it is adding the badge count every time a new notification comes in. For example, if the current badge count is 5 and a push notification comes with a badge count of 6, then instead of setting the badge count to 6, it will set it to 11, i.e. it adds 5 to 6 instead of replacing it.

It would be great if anyone could give some insight into the solution to this problem. iOS automatically updates the badge count using the payload but Android is not complying.

Regards, AJ

gitpayal commented 5 years ago

hi @ajinkyap7 ,

There could be various ways of implementing badgecount in android.One of the ways could be

Store badgeCount at server/database table. Everytime you send push notification, retrieve from server/database and add + 1 while sending it. In my application, I am using firebase to store badgecount for each user. And count on the mobile app comes correct

This solution is quite general. if you post your implementation , it would be more easier to get solution. It would be nice to see how your FCM payload from server looks like

ajinkyap7 commented 5 years ago

Hi @gitpayal ,

Thanks for the reply. At least the thread is still going :) Regarding the payload, we are using the following structure: { "data": { "title": "MESSAGE_TITLE", "body": "MESSAGE_BODY", "badge": "BADGE_COUNT" } }

In the above JSON, BADGE_COUNT is an integer so while sending the push notification, the actual badge count, for example 5, will be sent. This badge count is the current number that should be shown on the App Icon (or in the list after long-pressing the app icon from Android 8.0 onward)

We are also using Firebase Push Notifications in the application. Everything works well for iOS (payload is different though) but for Android the badge count doesn't get updated the way it should.

Let me know how you implemented the badge count in your application. It may prove to be helpful.

Regards, AJ

AdnanAshraf-gol commented 1 year ago

in your index.js file add this and make sure you PushNotification.configure outside of a component. this will work for android in background as well.


messaging().setBackgroundMessageHandler(
    async (remoteMessage) => {
             PushNotification.setApplicationIconBadgeNumber(parseInt(remoteMessage?.data?.badge));
    });