Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application icon badge number not increasing : Xcode

I am facing a problem with push notification application badge number value updation.

I am doing like:

-(void)application:(UIApplication *)application 
       didReceiveRemoteNotification:(NSDictionary *)userInfo {

       UIApplicationState state = [application applicationState];
       if (state == UIApplicationStateActive) {
          // do stuff when app is active

       }else{
          // do stuff when app is in background
          [UIApplication sharedApplication].applicationIconBadgeNumber = 
          [UIApplication sharedApplication].applicationIconBadgeNumber+1;  
               /* to increment icon badge number */
       }
 }

But, the icon is showing the badge number as '1' always, and it is not incrementing when more notifications are there/ one notification came after another.

Any advice is appreciable...

like image 864
Krishna Raj Salim Avatar asked Apr 18 '13 06:04

Krishna Raj Salim


3 Answers

The badge number is set by the operating system when you receive a JSON notification payload that resembles the following:

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

As you see, it's the server who is responsible for setting the correct number in the badge key. Your server needs to track or compute the number of pending notifications for each user and generate the badge number before sending the notification to Apple.

The client responsibility is to clear the notification badge, or decrement it, when the user sees a notification. The code to do so is

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

or

application.applicationIconBadgeNumber = 0; // Reset counter assuming the user is able to see all notifications at once.
like image 111
Daniel Martín Avatar answered Oct 29 '22 21:10

Daniel Martín


you can create a static variable instead, and assign it to the applicationIconBadgeNumber:

static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;
like image 39
coppertop Avatar answered Oct 29 '22 21:10

coppertop


I had the same problem and solved it by creating int. variable in class.h

like this :

a custom class.H

@property int badge;

a custom class.M

-(id)init{

_badge=0;

self = [super init];
if (self) {

}
return self;}

-(void)reminderCreator:(NSDate*)onTime withText:(NSString*)text{

_badge += 1;

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = onTime;
localNotification.alertBody = text;
localNotification.soundName=UILocalNotificationDefaultSoundName;

localNotification.applicationIconBadgeNumber=_badge;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }

so if you initialize this custom class, somewhere (maybe at your viewController) and then call the reminderCreator method several times to setup few localNotifications, it will assign incremented number to each notification.

+1 if this helped :)

like image 32
Archil Vardidze Avatar answered Oct 29 '22 22:10

Archil Vardidze