【问题标题】:ios7.1: push notification badge update issueios7.1:推送通知徽章更新问题
【发布时间】: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


【解决方案1】:

试试:

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

【讨论】:

    【解决方案2】:

    我最近遇到了这个问题,我不得不做一些演员:

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

    【讨论】:

    • 请注意语法可能有点不对,但你明白了
    • 我也是这样做的,但这个问题只在 iphone4 和 4s 中发现,在 iphone5 和 5s 中运行良好。
    • 您选择了NSInteger 吗?是 iPhone 型号还是 iOS 版本有所不同
    猜你喜欢
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多