简体   繁体   English

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

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

I'm using parse.com for sending push notifications between devices. 我正在使用parse.com在设备之间发送推送通知。

I'm sending the push message with badge increment value by 1. After opening the app the badge value will be set to zero. 我正在发送带有徽章增量值1的推送消息。打开应用程序后,徽章值将设置为零。 All the above functionalities are working fine. 以上所有功能均正常运行。 But, I can't get the badge value of the current installation. 但是,我无法获得当前安装的标志值。

As per the documentation for setting the current installation badge to zero by following code, 按照以下代码将当前安装徽标设置为零的文档

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

But, in my app the currentInstallation.badge is zero while opening the app after receiving the message. 但是,在我的应用中,收到消息后打开应用时, currentInstallation.badge为零。 ie, I need to directly set the currentInstallation.badge value to zero without checking the current badge value like below 即,我需要直接将currentInstallation.badge值设置为零,而无需检查当前的徽章值,如下所示

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

It's working fine. 一切正常。 But, with that badge value I need to do some other tasks inside my app. 但是,使用该徽章值,我需要在我的应用程序内执行其他一些任务。

Why the badge value is returning zero for me? 为什么徽章值对我返回零? What am I missing? 我想念什么?

PFInstallation.badge returns the last value of the badge, that was saved to the database. PFInstallation.badge返回保存到数据库的徽章的最后一个值。 It returns zero in your case, because the object was not yet refreshed from the server. 在您的情况下,它返回零,因为尚未从服务器刷新对象。

There are two ways of getting the badge value, before you kill it in your case: 有两种方法可以获取徽章的价值,然后再将其杀死:

Solution #1 (get the badge value from UIApplication) 解决方案1 (从UIApplication获取徽章值)

NSUInteger badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;

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

Solution #2 (refresh PFInstallation) 解决方案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