如何更新通知徽章编号?

时间:2014-05-30 08:55:22

标签: ios notifications apple-push-notifications appdelegate

很抱歉,当我从android开发跳转到IOS开发时,我想是否有任何函数会在收到通知时触发?或者我该如何处理通知?

这是我的应用中的应用代理。问题是每当收到消息时,如果应用程序在后台,它将不会触发didReceiveNotification函数,那么当应用程序处于后台时如何更新徽章编号?感谢

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(switch_tabbar)
                                                 name: @"switch_tabbar"
                                               object: nil];
    //self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
   // self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [window setRootViewController:tabBarController];
  //  self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];

    NSLog(@"%@",@"launch");

    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    if(launchOptions!=nil){
        NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
        //NSLog(@"launch %@",msg);
        [self createAlert:msg];
    }

    NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", nil];

    self.session = [[FBSession alloc] initWithPermissions:permissions];
    if (self.session.state == FBSessionStateCreatedTokenLoaded) {
        [self.session openWithCompletionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {

            [FBSession openActiveSessionWithReadPermissions:nil
                                               allowLoginUI:NO
                                          completionHandler:
             ^(FBSession *session,
               FBSessionState state, NSError *error) {
             }];

        }];
    }

    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"%@",@"receive");
    application.applicationIconBadgeNumber = 0;
    NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
    //NSLog(@"receive %@",msg);
    [self createAlert:msg];
}

- (void)createAlert:(NSString *)msg {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:[NSString stringWithFormat:@"%@", msg]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

2 个答案:

答案 0 :(得分:1)

在iOS中接收推送通知时,徽章计数会自动递增,我们无需在应用中更新徽章计数,

ApplePushService

在iOS推送通知格式

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
    "acme1" : "bar",
    "acme2" : 42
}

注意:“徽章”值必须为整数

答案 1 :(得分:0)

徽章值会自动从有效负载的值开始递增。无论它是什么,它都会变成关键“徽章”的新价值。

application.applicationIconBadgeNumber = badgeNumber仅在您的应用程序正在运行且未在后台模式下处于活动状态时执行。因此,当应用程序未处于活动状态时,没有其他方法可以在有效负载中发送计数以更改徽章计数。