我正在使用parse.com在设备之间发送推送通知。
我正在发送带有徽章增量值1的推送消息。打开应用程序后,徽章值将设置为零。以上所有功能都运行良好。但是,我无法获得当前安装的徽章值。
根据以下代码将当前安装标识设置为零的文档,
- (void)applicationDidBecomeActive:(UIApplication *)application {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
}
// ...
}
但是,在我的应用程序中,currentInstallation.badge
在收到消息后打开应用程序时为零。例如,我需要直接设置currentInstallation。在不检查当前标识值的情况下将标识值设置为零,如下
- (void)applicationDidBecomeActive:(UIApplication *)application {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
//if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
// }
// ...
}
工作得很好。但是,有了这个徽章值,我需要在我的应用程序中执行一些其他任务。
为什么徽章值为我返回零?我错过了什么?
pfininstallation。Badge返回保存到数据库的Badge的最后一个值。在您的示例中,它返回零,因为对象尚未从服务器刷新。
有两种方法获得徽章值,在你杀死它之前:
解决方案#1(从UIApplication获取徽章值)
NSUInteger badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
PFInstallation *installation = [PFInstallation currentInstallation];
installation.badge = 0;
[installation saveEventually];
NSLog(@"%d", (int)badgeValue);
解决方案#2 (refresh pfininstallation)
PFInstallation *installation = [PFInstallation currentInstallation];
[installation fetchInBackgroundWithBlock:^(PFInstallation *object, NSError *error) {
NSUInteger badgeValue = installation.badge;
installation.badge = 0;
[installation saveEventually];
NSLog(@"%d", (int)badgeValue);
}];