0

My app is based on creating the new event. Whenever the new event created by any member all devices get push notification for the new event. For that I had set the badge number on the Event button.

It is working fine when app is running but the delegate method doesn't work when the app is inactive or in background mode.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

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

    if(application.applicationState == UIApplicationStateInactive)
    {
        NSLog(@"inactive");
    }
    else if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"active");
    }
    else
    {
        NSLog(@"background");
    }
    NSString *badgenum=[[NSUserDefaults standardUserDefaults]objectForKey:@"eventbadges"];
    NSString *badgestr = [NSString stringWithFormat:@"%d",[badgenum intValue]+1];
    [[NSUserDefaults standardUserDefaults]setObject:badgestr forKey:@"eventbadges"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updatebadges" object:self];
}
Sudha Tiwari
  • 2,499
  • 26
  • 50
  • `-application:didReceiveRemoteNotification:` is not fired when in the background. You server pushing the notification should set the application badge number. – rckoenes Jun 17 '14 at 08:10
  • @rckoenes But how to show the badge number when app is in background? Where to set it? – Sudha Tiwari Jun 17 '14 at 08:14
  • In the push notification it self. not in you code. – rckoenes Jun 17 '14 at 08:20
  • 1
    See this http://stackoverflow.com/questions/18981945/badge-count-is-not-increasing-for-push-notification-always-badge-count-remains-1 And http://stackoverflow.com/questions/9742558/push-notification-badge-count-not-updating –  Jun 17 '14 at 08:24
  • @Sudha Server maintain a badge count for each device, You should not increase the badgecount programmatically in your app. – Yasika Patel Jun 17 '14 at 08:37
  • @rckoenes I don't need the badge number I just want to know that which method gets called when app is on background and push notification fires – Sudha Tiwari Jun 17 '14 at 08:37
  • No method will fire on receiving a push notification when you app is in the background. Unless it is background fetch push notification that tells iOS to notify your app. This is iOS 7+ only. You can read all of this in [Local and Push Notification Programming Guide](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html). – rckoenes Jun 17 '14 at 08:41

0 Answers0