iOS 7.1:推送通知徽章更新问题

4

我已经在我当前的项目中设置了推送通知。我按照所有推送通知所需的指示进行了操作,在[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);
}

And for getting 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

设置背景模式如下:

enter image description here

当我的设备连接到xcode并运行应用程序测试推送通知时,所有都正常工作。但是当我断开设备(iOS7.1)时,推送通知到达但徽章没有更新。否则,徽章在后台更新,并调用了所有方法。但是同样的事情在另一个我使用的设备上测试此应用程序,运行得很好。

我不明白我的错误在哪里,我在代码中做错了什么。是否有任何错误或其他问题?请帮我解决这个问题。


你的服务器会在apn中发送徽章吗? - cojoj
我正在设置徽章,就像我在上面的问题中提到的那样。 - Nitin Gohel
1
但是你正在使用远程通知...徽章是由服务器端管理的,而不是由设备上的你来管理。 - cojoj
2个回答

1

尝试:

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

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 
                 )
);

请注意,语法可能有些不正确,但您可以理解其意思。 - meda
我做的和你一样,但这个问题只在iPhone4和4s上出现,在iPhone5和5s上运行良好。 - Nitin Gohel
你将其转换为 NSInteger 了吗?是 iPhone 型号还是 iOS 版本造成了差异? - meda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接