gpt4 book ai didi

ios7.1:推送通知角标(Badge)更新问题

转载 作者:可可西里 更新时间:2023-11-01 06:08:09 25 4
gpt4 key购买 nike

我在我当前的项目之一中设置了Push Notification。我已遵循推送通知所需的所有说明。在 [tag: ios7] 中工作正常,但在 7.1 中,当我的应用程序处于后台模式时,角标(Badge)更新出现问题。

我的代码如下:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *str = [NSString
stringWithFormat:@"%@",deviceToken];
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*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

UIApplicationState state = [application applicationState];

// If your app is running
if (state == UIApplicationStateActive)
{

//You need to customize your alert by yourself for this situation. For ex,
NSString *cancelTitle = @"ok";
// NSString *showTitle = @"Get Photos";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:nil];
[alertView show];


}
// If your app was in in active state
else if (state == UIApplicationStateInactive)
{

}

[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

}

第一件事是,当我的应用程序返回到后台时,didReceiveRemoteNotification 没有被调用,所以我做了一些搜索并输入:-

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

然后像这样设置背景模式:-

enter image description here

当我的设备与 xcode 连接并且我运行应用程序测试推送通知时一切正常但是当我拔下设备时(iOS7.1)。然后推送通知到达但角标(Badge)未更新。否则,角标(Badge)在后台更新以及调用的所有方法。但同样的,我在另一台使用 iOS7 的设备上测试了这个应用程序,它运行良好。

我不明白我的错误在哪里,我在代码中哪里做错了。是否有任何错误或我不认为的问题所以请帮助我解决这个问题。

最佳答案

尝试:

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

关于ios7.1:推送通知角标(Badge)更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22690160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com