Blios
Blios

Reputation: 729

PushNotification badge number not showing when my app is not active?

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?

Upvotes: 2

Views: 5730

Answers (3)

Bharat
Bharat

Reputation: 3007

I had the same problem and from this question

If you also register UIRemoteNotificationTypeNewsstandContentAvailability then remove it and retry.

Upvotes: 2

Gajendra Rawat
Gajendra Rawat

Reputation: 3663

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];
}

Upvotes: -1

Haresh Ghatala
Haresh Ghatala

Reputation: 2006

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.

Upvotes: 1

Related Questions