Hi there,
I’m trying to update the badge on my app’s icon to a number depending on how many new messages my app has.
I’ve tried doing this through push notifications but this didn’t seem to work for capacitor and capacitor-fcm.
Or perhaps I did something wrong there.
Not to mention, my messages aren’t always sent with notifications. Sometimes a message is placed without notification, and in those cases the badge also needs to be updated, reflecting the amount of new messages.
In any case, what i’m trying to do is use:
To fetch information in the background from my api to see how many new messages there are since last time you had the app open.
It is then trying to use:
In order to set the badge when the app is in the background.
Setting the badge when the app is in the foreground works, but I can’t seem to find a way to do this while the app is in the background. AKA when using cordova-background-fetch.
Does anyone know of a good way to get this to work on ionic 4? Or at least how this should be handled?
Please let me know,
Why aren’t you using push notifications for this? Background tasks here are not really the right use case.
I couldn’t find much information about capacitor’s push notifications with badge usage. At least, not in the documentation.
The thing that came up when googling this issue was: Updating Badges in the Background?
Which suggests sending a different payload, but I did not know for certain whether this would also work with capacitor and the capacitor-fcm plugin for firebase cloud messaging topics.
Is there perhaps some kind of example available somewhere on how to set this up?
If you need to send the amount of badges with the notification payload, then how would one know which messages are new or not? Wouldn’t this be based on each user’s own interpretation of which messages they have read? I’m not quite sure how this would apply globally over locally.
How do other apps do this exactly? Can’t you for example say I want +1 notification badge instead of 4 or 5? Increase it incrementally? I would suppose this number differs for each user and not be the exact same for each user?
Thank you for the response,
Really appreciated.
Push notifications involve more than just badges… you’ll be connecting a push service to receive alerts for when certain users get a message and push a notification to that user.
https://capacitorjs.com/docs/apis/push-notifications
I already have a push service setup. I’m using fcm with topics. Each user can subscribe to either the news topic or events topic.
Whenever there is a new event or news message created on the backend (The backend user decides whether to send a notification, yes or no for the news or event message). When the backend user creates a notification it will be send to all users subscribed to that topic.
Which works.
However, I would like to know whether I can show a badge to the users whenever both events and news messages (including those without notification) are new to the user. Let’s say user A has 4 new events and news messages, and user B has 3 new events and news messages.
So I figured: I clear the badges when the app is started by the user, it then sets a date on when the badges were cleared, and each time a notification appears the badge gets updated to reflect the amount of new messages related to when the badges were last cleared.
For example: Increment by the amount of new messages since last saved local date.
How else would you implement something like this?
I can’t figure out how this would work otherwise. And how I would create such a system.
Or am I generally confused on how badges work? I’m pretty certain other apps use it this way as well.
EDIT:
What I could do is send an api request from cordova-background-fetch to the backend with the device id of the user, and then let the backend create a notification for that specific user saying how many new messages that user has.
However this is very privacy sensitive. Since it is then possible to determine which device id which user has if the backend were to be infiltrated.
I’ll try to figure out a solution to this, but it’s not easy.
hey i am also looking for a way to update badge count, My push notification is working we are using capacitor’s Push notification. Need a way to get notification count and increase the count on new notification received … Any help would be much appreciated
The problem you are facing is the same one I am facing for a different function. I am trying to add Action buttons to my Push notifications by running LocalNotifications code when a push notification is received.
The problem here is that in PushNotifications.addListener(‘pushNotifcationReceived’), will only trigger in the foreground and will not trigger in the background.
I do not know why nothing is being done about this but this is the problem. So yes, ideally you don’t want to run a background service, but it’s the only way as a workaround for increasing the badge count.
See the Github request here: bug: PushNotifications not triggering code within `pushNotificationReceived` (data notification) · Issue #200 · ionic-team/capacitor-plugins · GitHub
Hopefully now that its on deck with Capacitor Engineering something will get resolved
We’re having this exact same issue - on iOS the pushNotifcationReceived will not trigger in background
I’m having same issue using cordova 8, any update here?