janagowtham
janagowtham

Reputation: 167

Update Badge count of app icon according to unread message in ios

I need the count value of unread as badge number.and badge number of app icon has to reduced and increase according to the unread message count.(increase if new unread message ,reduced if unread message is read)

->["Unread" is the count of unread messages.]

NSString *unread =[[NSUserDefaults standardUserDefaults]valueForKey:@"unread"];                 
int badge = [unread intValue];
[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];

Upvotes: 0

Views: 2545

Answers (1)

user3339357
user3339357

Reputation: 292

Usually the badge number is set by the OS when you receive JSON in the form of:

{
    "aps" : {
        "alert" : "New notification!",
        "badge" : 2
    }
}

So the server sets the badge number, meaning that you have to keep track of how many notifications a user has.

On the client side, you have to clear the notification like this:

application.applicationIconBadgeNumber = application.applicationIconBadgeNumber - 1; // Decrement counter

Or you can just set them all to 0 and assume they're all read once app is opened, like this:

application.applicationIconBadgeNumber = 0;

Upvotes: 2

Related Questions