推送通知徽章数量不更新

3
这是我的苹果推送通知代码,当应用程序正在运行并且有通知到达时,我会增加徽章计数,并在单击主屏幕按钮时获得所需结果。但是,当我没有运行我的应用程序并且通知到来时,它没有自动增加徽章计数,仍然保持为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];


}

可能是推送通知徽章未到达的重复问题。 - jrturton
我没有得到答案,所以我在这里发布了问题。 - Syed Faraz Haider Zaidi
1
这不是它的工作方式。编辑您的原始问题或添加赏金。在原始问题中,您是否仍然说它正在工作? - jrturton
好的,下次我会做的,谢谢。 - Syed Faraz Haider Zaidi
这里是完全相同问题的解决方案 http://stackoverflow.com/questions/26117430/pushwhoosh-remote-notification/26119399#26119399 - user4034301
1个回答

4

苹果不会为你跟踪数据。它只显示你告诉它的内容。因此,你需要在服务器上存储计数,然后在发送提醒时告诉苹果新的徽章号码。通常这是通过应用程序启动时向服务器打电话告诉其将未读通知的计数归零来完成的。


我的服务器如何知道用户是否已读取某条消息? - Syed Faraz Haider Zaidi
2
这是你需要自己想办法解决的问题。简单的方法是在打开应用时将两者都设置为0。 - coneybeare
我是通过将代码放在应用程序完成启动的方法中来实现的:[UIApplication sharedApplication].applicationIconBadgeNumber = 0; - Syed Faraz Haider Zaidi
您还需要告诉服务器重置服务器值,以便下一条通知从1开始。 - coneybeare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接