如何更新IOS通知中的徽章编号?

时间:2014-06-01 18:42:42

标签: ios objective-c amazon-web-services notifications push-notification

我最近使用Amazon SNS推送我的IOS应用程序的通知。

它运作良好,我遇到的唯一问题是当我收到通知时,徽章编号将不会更新,这是我的实施方式:

首先,我按照这里的例子 https://aws.amazon.com/articles/9156883257507082 ,我只提取SNS部分,这意味着我只改变我的应用程序中的appdelegate。以下是教程中的示例代码。

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0;
    NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
    NSLog(@"%@",msg);
    [[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}


-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Register for push notification
    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    if(launchOptions!=nil){
        NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
        NSLog(@"%@",msg);
        [[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
    }

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    Message_BoardViewController *boardViewController = [Message_BoardViewController new];
    UINavigationController *navigationController = [UINavigationController new];

    navigationController.navigationBar.translucent = NO;

    [navigationController pushViewController:boardViewController animated:NO];
    [boardViewController release];

    self.window.rootViewController = navigationController;
    [navigationController release];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

    [self.window makeKeyAndVisible];

    // Logging Control - Do NOT use logging for non-development builds.
#ifdef DEBUG
    [AmazonLogger verboseLogging];
#else
    [AmazonLogger turnLoggingOff];
#endif

    [AmazonErrorHandler shouldNotThrowExceptions];

    return YES;
}

正如您所看到的,徽章编号将始终为0,而​​且我发现来自SNS的JSON传入通知没有徽章字段但只有警报字段,当应用程序也在后台时,不会调用didReceiveRemoteNotification。 那么,我该如何修改代码以更新徽章?谢谢你的帮助。

0 个答案:

没有答案