2

I'm using Urbanairship for APNS in my app and everything going well but app badge number is not displaying when my app is not running, however when app is active and push is received then badge number is displayed over app icon.

Here is my code as following:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
     UAConfig *config = [UAConfig defaultConfig];
        [UAirship takeOff:config];
        // Request a custom set of notification types
        [UAPush shared].notificationTypes = (UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert |
                                             UIRemoteNotificationTypeNewsstandContentAvailability);

        return YES;
    }
    - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

        NSString *str = [NSString stringWithFormat: @"Error: %@", err];
        NSLog(@"%@",str);

    }

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        NSLog(@"Received notification: %@", userInfo);
        //[self addMessageFromRemoteNotification:userInfo];

        NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
        NSLog(@"my message-- %@",alertValue);
        int badgeValue= [alertValue intValue];

        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];


    }
- (void)applicationDidBecomeActive:(UIApplication *)application {

    application.applicationIconBadgeNumber = 0;
}

EDIT: i also tried with -

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSInteger badgeNumber = [application applicationIconBadgeNumber];
    [application setApplicationIconBadgeNumber:++badgeNumber];
}

but still no luck, what I'm doing wrong?

Blios
  • 719
  • 1
  • 16
  • 30
  • did you check your server side there is properly setting badge value.? what is log print `[alertValue intValue]`? – Nitin Gohel Mar 24 '14 at 05:29
  • This is the log- Received notification: { "_" = "eHa-4LMVEeOdIJDiugJkgA"; aps = { alert = "This is a test push from MAdept"; badge = 21; }; } and [alertValue intValue] is 21. – Blios Mar 24 '14 at 05:32
  • try ones with put static number `[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];` what happend with this..? – Nitin Gohel Mar 24 '14 at 05:36
  • still no luck, i think problem is not here because it showing when push received while app is running and i press the home button without clicking on push alert. – Blios Mar 24 '14 at 05:40
  • Beside your problem, make sure when you do this `application.applicationIconBadgeNumber = 0;` server also resets the count. – NeverHopeless Mar 24 '14 at 05:44
  • is anyone there who can notice the problem? – Blios Mar 24 '14 at 06:10

3 Answers3

2

I had the same problem and from this question

If you also register UIRemoteNotificationTypeNewsstandContentAvailability then remove it and retry.

Community
  • 1
  • 1
Bharat
  • 2,987
  • 2
  • 32
  • 48
  • For iOS 8 and higher you must use registerUserNotificationSettings method [see here](http://stackoverflow.com/questions/24021113/how-to-correctly-set-application-badge-value-in-ios-8) – Libor B. Mar 23 '16 at 11:08
1

Your JSON needs to be in following format.

What you have won't properly convert into an NSDictionary.

{
    "aps" : {
        "alert" : "You got your Notification",
        "badge" : 1,
        "sound" : 'default'
    },
    "acme1" : "bar",
    "acme2" : 42

}

Please reference the Push Notification Document.

Haresh Ghatala
  • 1,996
  • 17
  • 25
-1

Use this code may it helps you.

UIApplicationState state = [application applicationState];

if (state == UIApplicationStateActive)
{

   NSLog(@"Received notification: %@", userInfo);
    //[self addMessageFromRemoteNotification:userInfo];

    NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
    NSLog(@"my message-- %@",alertValue);
    int badgeValue= [alertValue intValue];

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
}
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36