推送通知徽章计数未更新

时间:2012-03-16 18:20:48

标签: objective-c ios ios4 apple-push-notifications

这是我的苹果推送通知代码,当应用程序正在运行并且通知即将增加徽章计数并在我点击应用程序图标上的主页按钮时获得所需结果。但是当我没有运行我的应用程序并且通知到来时,它没有自动增加徽章计数并且仅保留为1.值1来自服务器。任何人都可以指出我做错了什么。提前谢谢。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    userMessageCounter = @"0";
    postType = 0;
    joinedStreamChecker = 0;
    OwnerValue = 0;
    pushValue = 1;
    badgeValue =0;

    // Override point for customization after application launch.

    // Add the navigation controller's view to the window and display.
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];


    [[UIApplication sharedApplication] 
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeBadge | 
      UIRemoteNotificationTypeSound)];


    //[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
    //(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone) 
    {
        pushValue = 0;


        NSLog(@"notification off");
    }

    return YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken1 { 

    NSString *str = [NSString 
                     stringWithFormat:@"%@",deviceToken1];
    NSLog(@"%@",str);

    self.deviceToken = [NSString stringWithFormat:@"%@",str];
    NSLog(@"dev --- %@",self.deviceToken);
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"dev --- %@",self.deviceToken);


}

- (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);
    badgeValue= [alertValue intValue];
    [UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
    //badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
    //[UIApplication sharedApplication].applicationIconBadgeNumber=0;
    //[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];


}

1 个答案:

答案 0 :(得分:4)

Apple不会为您跟踪您的数据。它只显示你告诉它的内容。因此,您必须将计数存储在服务器上,然后在发送警报时告知Apple新的徽章编号。通常,这是通过在启动时让应用程序电话回家来告知您的服务器将未读通知的数量清零来完成的。