Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

APNS Background handling for badge counter

I am integrated APNS in my app, The requirement is to maintain notification count when app in background. For example we got notification in background in there is key counter count , i,e changing dynamic in every notification, Can it possible to handle in iOS when app is background or app has forcefully closed.

like image 515
Prabhat Pandey Avatar asked Oct 15 '25 04:10

Prabhat Pandey


1 Answers

This is the APNS payload from back end server.

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
    "acme1" : "bar",
    "acme2" : 42
}
  1. The value for key badge is automatically considered as badge count.On ios app side no need to calculate or handle the count.
  2. In above example 9 is the badge count.So your app icon will show 9.
  3. NOTE While your app is close u can't handle badges on your own.Thats why we are using badge key from APNS Payload
  4. For better clarification about notification see documentation

EDIT: if you want to reduce the badge count on your own.Decrement the count and update it yourself.as follows

NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber
numberOfBadges -=1;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

or else make the count to 0, so that badge icon will be disappeared.Add the below code in ** applicationDidBecomeActive**

application.applicationIconBadgeNumber = 0;
like image 115
Gokul G Avatar answered Oct 17 '25 17:10

Gokul G