我在我当前的一个项目中设置了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
,运行良好。
我无法理解我的错误在哪里以及我在代码中做错了什么。是否有任何错误或我不认为所以请帮助我。
答案 0 :(得分:1)
尝试:
int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];
答案 1 :(得分:0)
我最近遇到过这个问题,我不得不做一些演员:
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
)
);