简体   繁体   English

推送通知徽章计数未更新

[英]Push Notification Badge Count Not Updating

this is my code for apple push notification, when the application is running and notification coming i am incrementing the badge count and getting desired result when i click home button, on app icon.这是我的苹果推送通知代码,当应用程序正在运行并且通知即将到来时,当我单击应用程序图标上的主页按钮时,我会增加徽章计数并获得所需的结果。 but when i am not running my application and notification comes, it didn't auto increment badge count and remains to only 1. the value 1 is coming from server.但是当我没有运行我的应用程序并且通知出现时,它没有自动增加徽章计数并且只保持为 1。值 1 来自服务器。 can any one point out where i am doing wrong.任何人都可以指出我做错了什么。 thanks in advance.提前致谢。

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


}

Apple does not keep track of your data for you. Apple 不会为您跟踪您的数据。 It only shows what you tell it to.它只显示你告诉它的内容。 Thus, you have to store the count on your server, then tell apple the new badge number when you send the alert.因此,您必须将计数存储在您的服务器上,然后在发送警报时告诉苹果新的徽章编号。 Typically this is done by having the app phone home when launched telling your servers to zero out the count of unread notifications.通常,这是通过在启动应用程序时通知您的服务器将未读通知的计数归零来完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM