我已经在我当前的项目中设置了Push Notification。我遵守了推送通知所需的所有指示。这在标记中运行得很好: 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);
}和for responce
-(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连接时,当我运行应用程序测试推送通知时,当我拔出设备( is 7.1)时,所有这些都可以正常工作。然后推送通知到达,但徽章没有更新。否则,该徽章更新的背景以及所有的方法调用。但同样的事情,我测试这个应用程序在我的另一个设备与iOS7工作良好。
我不明白我的错误在哪里,我在代码中做错了什么。有什么bug或什么我不认为,所以请帮助我解决这个问题。
发布于 2014-04-09 09:54:59
尝试:
int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];发布于 2014-04-15 06:05:02
我最近有这个问题,我不得不做一些演员:
NSDictionary *apsData = [userInfo objectForKey:@"aps"];
NSInteger badgeNumber = (NSInteger)[[apsData objectForKey: @"badge"] intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + badgeNumber;我使用PHP将数据发送到苹果服务器,所以我也在那里做了一个转换:
$playload = array('aps'=>array(
'message'=> 'some message',
'badge'=> (int)$badge
)
);https://stackoverflow.com/questions/22690160
复制相似问题