0

Is there any way to increment a badge by one every time a push notification is received and the app is closed and not in the background?

I am currently managing the badge count on the server side and including the updated badge count in the payload. However in the case where multiple notifications need to be sent (such as a chat room situation), this is tedious and expensive on the server. Instead of passing an array of deviceToken's to the apns server I will need to loop through each device with the badge number for that device.

I would prefer to increment the badge count locally on the device when the push notification is received in ALL CASES including when the app is closed and not in the background.

alionthego
  • 8,508
  • 9
  • 52
  • 125
  • While I don't know, logically it seems you can't. You should check when the application is (re)launched. What type of OS function - particularly iOS - would be able to handle such a thing? –  Apr 17 '18 at 01:11
  • If your app qualifies as a VoIP app then you can use Pushkit to relaunch the app in the background via a silent push notification. – Paulw11 Apr 17 '18 at 01:24
  • without qualifying as VoIP there is no way to increment the badge count when the app is not in foreground or background. you can only set a certain badge value as part of the payload. to achieve this you will need to increment a badge field for your user in the database on your backend every time the push notification is sent in response to your action. when the user downloads the objects on the server-side, then reset the badge count as part of the get objects server code. – alionthego Nov 28 '18 at 23:40

1 Answers1

2

If you want to update the badge number upon receiving a notification, then you need to set the Badge property of the json push notification to the desired number as follows:

{
    "aps": {
        "alert": "Test Push Notification",
        "sound": "yourSound.aiff",
        "Badge": "desiredNumber"
    }
}

desiredNumber will be the desired badge count

pkc456
  • 8,350
  • 38
  • 53
  • 109