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?

有帮助吗?

解决方案

I had the same problem and from this question

If you also register UIRemoteNotificationTypeNewsstandContentAvailability then remove it and retry.

其他提示

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.

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];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top