Push-Notification Badge auto increment

46,903

Solution 1

Nope, you'll have to track this on the server side. If you don't include any value for badge, it will get removed completely.

Of course though this is only if the user receives the notification and the app isn't running/they choose not to launch it. If the user launches the app or already had it running you can do whatever you want in regards to incrementing.

UPDATE March 2014: See comments for a possible update. I haven't done pushes in several years so haven't been able to verify this myself.

Solution 2

It's sort of possible but there's a trade-off.

You can always send up the unread total as an add-on JSON value as part of the push payload (push ignores keys it doesn't explicitly understand). Once the user opens the app, read the value and adjust the badge programmatically yourself via UIApplication's applicationIconBadgeNumber property.

The problem with doing it that way is that push adjusts the badge value even if the user doesn't open the app (i.e. when they get the notice and the user hits 'Cancel' instead of 'View'). In those cases your badge won't change, but as soon as they run the app (if they hit 'View') then your app can set it right.

Solution 3

It is now possible to have the client auto-increment the badge using a UNNotificationServiceExtension.

Extensions have the ability to modify notification payloads before iOS displays them. In summary, store a badge counter in UserDefaults and modify the notification's badge count as needed. You'll need to add the App Groups capability to share User Defaults.

Here's a detailed step-by-step guide: https://prodocs.cometchat.com/docs/ios-increment-app-icon-badge-count

Solution 4

You can try out App42 backend services which provide auto increment of push badge count which is maintained on the server side. For more details you can follow the link of blog. Here is the blogpost conent:

Here are the few use cases that can be achieved through auto incremental badge count in App42 Push Notification.

For auto increment of push badge by 1, you need to send push message as shown below.

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string message= "{'badge':'increment'}";
pushNotificationService.SendPushMessageToUser(userName,message, new UnityCallBack())

N.B: The sample explained is for Unity/C# but the same process can be applied on others too.

If you want to stipulate any number for the badge or want to reduce the badge count to zero, you can use this method to update the count as the notification gets clicked by the user. You have to call updatePushBadgeforDevice or updatePushBadgeforUser in this case.

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string deviceToken = "DeviceToken";
int badges = 10; // For clear count make it 0 
pushNotificationService.UpdatePushBadgeforDevice(userName, deviceToken, badges,  new UnityCallBack());

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
int badges = 10; // For clear count make it 0
pushNotificationService.UpdatePushBadgeforUser(userName, badges,  new UnityCallBack());

updatePushBadgeforDevice – This method is used to update push badge count of a particular device registered by the user .

updatePushBadgeforUser – This method is used to update push badge count of all the devices that a user procures. In this case, we are assuming that the user has multiple devices registered under his name.

Share:
46,903
natanavra
Author by

natanavra

Fullstack developer and Jedi Master.

Updated on June 01, 2020

Comments

  • natanavra
    natanavra almost 4 years

    I've been implementing the push service to my application, and I've been thinking about the application's badge. My app is a mail app (sorta) and I want to notify the user via push for new messages added to the inbox, I want the badge = number of new messages in the inbox.

    I thought of doing it server sided (provider) checking for new messages and sending the number as the badge.

    The question is: Is there a way to auto-increment the application's badge, without having to calculate the badge value server sided and afterwards sending it as a part of the push payload to the APSN?

    Maybe there's a way to send in JSON badge field some variable like "++" or something like that. Any hack for that? Or do I need to go with the counting system server-sided??