Skip to content

arindamnayak/cordova-fcm-badge-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status : CircleCI

cordova-fcm-badge-test

This is a sample project that demonstrates, how corodava-fcm-plugin shows FCM notification with badge count on app icon. Steps to run this project. Since android does not support badge count on icon, this repo is only to demo, how to make it work for android only. And iOS has inbuilt support for this. So I am skipping that now.

  • First setup FCM project in FCM console. Here is the link that has steps to set up.
  • Then place the google-services.json file at root directory level.
  • Then create a request capture url at requestb.in and use that url in www/js/index.js/saveToRemote . This will save token there and we will use that token to send notification for testing the badge set purpose.
  • Assuming you have cordova, you need to add android platform using "cordova add platfrom android@4.1.0".
  • Then generate APK using "cordova build android".
  • Get the APK from platforms\android\build\outputs\apk\android-debug.apk
  • Since I have used ShortcutBadger , that has limited android support such as htc/sony/samsung/asus/LG etc.., you need to install the APK in one of those mobile.
  • On opening app, you will get token in requestb.in website.
  • Now, get service key from FCM using this link.
  • There are 3 cases to make it work (the badge count on icon). Anyway notification works for all, but for badge here are the cases.

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.

Explanation

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

Conclusion

To show badge count in app icon, you need to merge this PR. Notification payload 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.

This is how it will look.

example