Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to show badge count on app icon in supported android devices #279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

arindamnayak
Copy link

@arindamnayak arindamnayak commented Apr 3, 2017

@fechanique, please review this.

Why?

This is a big pain to support badge count(e.g. iOS shows badge count i.e. the number of unread msg just above app icon) for android mobile with Cordova FCM plugin, to do so, we have to use this library separately. So why not include that library directly here, so that other users can benefit from this.

What

I have added code, that enables badge count in android mobiles. I have used this library which adds the badge to app icon, that works in specific mobiles ( such as Sony, Samsung, Asus, HTC etc. ).

How

You need to include "badge: XX" in the data part of notification payload in server side code. This is because, when you send the notification, the "badge" key inside notification is not meaningful to android FCM library, they don't get/deserialize the value in android code. So you have to include badge in the data part of notification payload. This issue with firebase is mentioned here.

Example

I have also created a sample project, that uses my version of fcm-plugin ( Pull request changes). Here is the link to that project.
This is how it will look.
example

Detailed explanation

As per FCM document, when you send notification+data, onMessageReceived will not work if the app is in the background. FCM will only show notification in the system bar, and on tapping the notification, onMessageReceived will be called. But when you send only data, onMessageReceived will be called, and that is why badge will be updated. For more help on FCM document, visit this.

Note: This will work for specific android mobiles.

There are 3 cases to make it work (the badge count on the icon). Anyway notification works for all, but for badge here are the cases.

  • App in the foreground.

Just send the message in request payload, include "badge" in data with the required count. It will show directly. Here is the sample payload.

  • App is background - case -1

Repeat above step, on opening notification, it will update badge count in-app.

  • App is background - case -2

Now while sending a notification, send only data part not notification part. This way it will directly update badge count. Here is the sample payload.

Alternative

You can intercept notification using notification service listener, the link for google document is here. But this works for android version KITKAT onwards and for below, you can intercept using AccessbilityService. I have tried former one with "NotificationListenerService". I faced one issue, by default the Cordova app is not able to intercept notification, I need to enable in "notification access" in setting option. But here, you neither get badge value from "notification" nor badge from "data" part of request payload, you only get package name, title, text, icons etc.. The other option is to somehow pass badge count in the title with some delimiter separated, and parse it on receiving, then show actual title and use the badge. Again, for android you can not preprocess notification i.e. you can't manipulate once received. This forum explains same. So, we can not opt for this alternative.

Conclusion

To show badge count in app icon, we need this PR changes or get the merged version of cordova-fcm-plugin. Notification payload(sample one) request has to contain badge in the data part of JSON. To ensure it works for all scenario, just send a separate call which has only data part.

@arindamnayak arindamnayak changed the title Added ShortcutBadger for badge support in android Added support to show badge count on app icon in supported android devices Apr 4, 2017
@arindamnayak
Copy link
Author

arindamnayak commented Apr 28, 2017

@fechanique can you review this PR? @fechanique, "cordova-plugin-firebase" supports badge using "ShortcutBadger", which I used in this PR and if this PR is merged user will be able to show badge out of the box using this plugin.

@Wolf00Bomber
Copy link

Any progress on this feature? Will Google/Firebase respond on this PR, whether this will be taken up or not??

@anouarchattouna
Copy link

Thank you @arindamnayak for your proposal. You concluded: To ensure it works for all scenario, just send a separate call which has only data part. which means that we need to two POST requests each time, the first one with notification payload and the second one with data payload (including the badge value). The value of the badge field will update the the badge count on the icon each time.

What if you wanted to just increment the badge count by 1?
Imagine the user has 3 unread notifications ==> badge count = 3, then from your backend you send a push notification (2 POST requests as explained before), logically the badge count need to be incremented by one ==> badge count = 4. But from your payload you don't have that information!

My question is how to know the badge count before sending the push requests ? Or how to increment the badge count if we suppose that every push request will have badge field = 1 ?

@arindamnayak-spigit
Copy link

@anouarchattouna, thanks for the question.

Regarding the question

how to know the badge count before sending the push requests?

Ideally sending a push notification is disconnected process, where the server sends a message to the mobile app. Here server has to maintain what value needs to be sent to the client which includes badge. Even if it is a simple increment, the server has to calculate what is unread notification and send it in the request.

E.g. I use Gmail as in web app and in the mobile app. Say I have 4 unread message, mobile app shows 4 as badge count. Now, I logged into Gmail using the desktop browser, I read one message, here server has to calculate there are 3 unread messages, it has to update the same in the mobile app. Thoughts?

I can see there is an open issue( #385 ) for this repo.

@demym
Copy link

demym commented Mar 5, 2018

Hi, i try sending only the data part to have Android badge updated (my Android launcher supports badge). But no way with fcm plugin; it worked with phonegap-push-plugin... Any idea ?

@arindamnayak
Copy link
Author

arindamnayak commented Mar 8, 2018

@demym , have you taken code from this PR or from master branch? After this PR is merged, then the feature will be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants