Prabhat Pandey
Prabhat Pandey

Reputation: 277

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.

Upvotes: 4

Views: 10324

Answers (2)

Gokul G
Gokul G

Reputation: 2096

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;

Upvotes: 8

Buntylm
Buntylm

Reputation: 7373

For managing the notification counter you have to setup functionality at server. lets take Facebook example if you are not reading the new notification the counter keep increase by one and once you click on notification it comes back to zero. so whenever you read new notification it also managed on server side whether is user opened it or not.

lets say there are 3 notifications user di't see than for next one counter will be 4.

And once your receive the count from server with update, handle it like if all updates are viewed application badge counter set to 0

this is my understanding as i have implemented the same in my iOS application if anyone has any good solution. please suggest.

I hope this picture(source google) will give the complete understanding.

enter image description here

Upvotes: 3

Related Questions