繁体   English   中英

如何在iOS的parse.com中获取当前安装的徽章值?

[英]How to get the badge value of current installation in parse.com for ios?

我正在使用parse.com在设备之间发送推送通知。

我正在发送带有徽章增量值1的推送消息。打开应用程序后,徽章值将设置为零。 以上所有功能均正常运行。 但是,我无法获得当前安装的标志值。

按照以下代码将当前安装徽标设置为零的文档

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  if (currentInstallation.badge != 0) {
     currentInstallation.badge = 0;
      [currentInstallation saveEventually];
  }
  // ...   
}

但是,在我的应用中,收到消息后打开应用时, currentInstallation.badge为零。 即,我需要直接将currentInstallation.badge值设置为零,而无需检查当前的徽章值,如下所示

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  //if (currentInstallation.badge != 0) {
     currentInstallation.badge = 0;
     [currentInstallation saveEventually];
  // }
  // ...
}

一切正常。 但是,使用该徽章值,我需要在我的应用程序内执行其他一些任务。

为什么徽章值对我返回零? 我想念什么?

PFInstallation.badge返回保存到数据库的徽章的最后一个值。 在您的情况下,它返回零,因为尚未从服务器刷新对象。

有两种方法可以获取徽章的价值,然后再将其杀死:

解决方案1 (从UIApplication获取徽章值)

NSUInteger badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;

PFInstallation *installation = [PFInstallation currentInstallation];
installation.badge = 0;
[installation saveEventually];
NSLog(@"%d", (int)badgeValue);

解决方案2 (刷新PFInstallation)

PFInstallation *installation = [PFInstallation currentInstallation];
[installation fetchInBackgroundWithBlock:^(PFInstallation *object, NSError *error) {
    NSUInteger badgeValue = installation.badge;
    installation.badge = 0;
    [installation saveEventually];
    NSLog(@"%d", (int)badgeValue);
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM