[英]Push Notification Badge Count Not Updating
这是我的苹果推送通知代码,当应用程序正在运行并且通知即将到来时,当我单击应用程序图标上的主页按钮时,我会增加徽章计数并获得所需的结果。 但是当我没有运行我的应用程序并且通知出现时,它没有自动增加徽章计数并且只保持为 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];
}
Apple 不会为您跟踪您的数据。 它只显示你告诉它的内容。 因此,您必须将计数存储在您的服务器上,然后在发送警报时告诉苹果新的徽章编号。 通常,这是通过在启动应用程序时通知您的服务器将未读通知的计数归零来完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.