我在ios应用程序上使用安装,推送和徽章(parse.com sdk 1.7.2.2) 几天前我注意到了一些事情, 如博客文章(旧版http://blog.parse.com/announcements/badge-management-for-ios/)
中所述,将徽章重置为0的代码// Clear badge if needed
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!succeeded) [ErrorHandler handle:@"save installation failed" forError:error];
}];
}
不再有用了, 一切都很好(没有解析错误),但徽章计数保持在数据库中的旧值
我第二次尝试了一下,似乎在一段时间内效果更好:
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (UIApplication.sharedApplication.applicationIconBadgeNumber > 0 || currentInstallation.badge > 0) {
UIApplication.sharedApplication.applicationIconBadgeNumber = 0;
currentInstallation.badge = 0;
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (!succeeded) [ErrorHandler handle:@"save installation failed" forError:error];
}];
}
但这不起作用,
任何想法?
答案 0 :(得分:3)
这是我的快速代码,它有效:
// Resets badge number in parse
var installation = PFInstallation.currentInstallation()
if installation.badge != 0 {
installation.badge = 0
installation.saveInBackgroundWithBlock(nil)
}
// Resets badge number in app
if application.applicationIconBadgeNumber > 0 {
application.applicationIconBadgeNumber = 0
}
答案 1 :(得分:0)
install.badge和application.applicationIconBadgeNumber setter之间似乎存在依赖关系。确保始终设置安装似乎可以缓解这个问题。
let pcur = PFInstallation.currentInstallation()
print("current badge = \(pcur.badge)")
if (pcur.badge != 0){
pcur.badge = 0
pcur.saveInBackgroundWithBlock({
(succeeded,error) in
print("badge save success = \(succeeded)")
application.applicationIconBadgeNumber = 0
})
}
答案 2 :(得分:0)
使用Parse(1.14.2),Xcode 8和ios 10,添加:
UIApplication.shared.applicationIconBadgeNumber = 0
AppDelegate 类中的 applicationDidBecomeActive 方法内的也会将解析服务器上的徽章重置为零。