当我的应用未激活时,推送通知徽章编号未显示?

时间:2014-03-24 05:23:20

标签: ios iphone objective-c apple-push-notifications urbanairship.com

我在我的应用程序中使用Urbanairship用于APNS,一切顺利但我的应用程序未运行时未显示应用程序徽章编号,但是当应用程序处于活动状态且已收到推送时,则会显示徽章编号应用程序图标。

以下是我的代码:

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

编辑:我也试过 -

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

但仍然没有运气,我做错了什么?

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,来自this question

如果您还注册UIRemoteNotificationTypeNewsstandContentAvailability,请将其删除并重试。

答案 1 :(得分:1)

您的JSON需要采用以下格式。

您所拥有的内容无法正确转换为NSDictionary。

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

}

请参考Push Notification Document

答案 2 :(得分:-1)

使用此代码可能会对您有所帮助。

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