【发布时间】:2014-05-06 13:50:24
【问题描述】:
我在我当前的一个项目中设置了Push Notification。我已遵循推送通知所需的所有说明。在 [tag: ios7] 中工作正常,但在 7.1 中,当我的应用程序处于后台模式时,我的徽章更新出现问题。
我的代码如下:-
- (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
并设置背景模式,如:-
当我的设备与 xcode 连接并运行应用程序测试推送通知时,一切正常,但是当我拔下设备时(iOS7.1)。然后推送通知到达,但徽章没有更新。否则,该徽章会在后台更新以及调用的所有方法。但是同样的事情,我用iOS7 在我的另一台设备上测试了这个应用程序,运行良好。
我不明白我的错误在哪里以及我在代码中做错了什么。是否有任何错误或我认为没有什么所以请帮助我解决这个问题。
【问题讨论】:
-
您的服务器是否在
apn中发送徽章? -
正如我在上面的问题中提到的那样,我正在从我身边设置徽章。
-
但是您正在使用远程通知...管理徽章的是服务器端,而不是您在设备上。
标签: ios push-notification ios7.1