简体   繁体   中英

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.

I'm sending the push message with badge increment value by 1. After opening the app the badge value will be set to zero. 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. ie, I need to directly set the currentInstallation.badge value to zero without checking the current badge value like below

- (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. 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)

NSUInteger badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;

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

Solution #2 (refresh PFInstallation)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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