0

My app receives both types of notifications, push and local, and I don´t know what the best way to manage the badge number is. In case of local notifications, I increment the badge counter whenever I create one, but in the case of push notifications... if I'm not wrong, the delegate method didReceiveRemoteNotification: is only called if the user launches the app by tapping the alert or the banner, or if the app is active. Otherwise, the value for the badge is taken from the notification's payload, isn't it? So... how could I always have an updated badge value taking into account the local notifications + remote notifications received?

Thanks

AppsDev
  • 12,319
  • 23
  • 93
  • 186

1 Answers1

0

You got response in Remote notification as below.

Connection OK sending message :{"aps":{"alert":"pushnotification testing","content-available":"","badge":"1","sound":"default"}} 134

In AppDelegte.h class

Take one integer variable int i=0;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationIconBadgeNumber = 0;

    UILocalNotification *localNoty=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNoty) {
        NSLog(@"Recieved Notification %@",localNoty);
    }

    return YES;
}

// Received notification

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running

    app.applicationIconBadgeNumber=i++;
    NSLog(@"Recieved Notification %@",notif);
}

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

#if !TARGET_IPHONE_SIMULATOR
    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);

    // set it to Zero will clear it.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

#endif
}

-(void) pw_setApplicationIconBadgeNumber:(NSInteger) badgeNumber
{
    [UIApplication sharedApplication].applicationIconBadgeNumber +=badgeNumber;
}

// Set badges nil when apps become active.

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    application.applicationIconBadgeNumber =nil;

}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • Thanks. `pw_setApplicationIconBadgeNumber:` is a method to use with `Pushwoosh`, right? I'm not using it. And the problem I find is that my app displays push notifications as well as local notifications and, even if my server keeps track of the badge number for the push notification's it needs to send, I don´t know how to also add the number of the current local notifications launched... – AppsDev Jul 07 '14 at 06:28