Reputation: 8444
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?
Upvotes: 1
Views: 1929
Reputation: 59
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);
}];
Upvotes: 1