Parse.com和ios的安装和徽章问题

时间:2021-03-27 16:27:20

I use installation, push and badge on my ios app (parse.com sdk 1.7.2.2) and I noticed something few days ago, the code that reset the badge to 0 as explained in the blog post (old one http://blog.parse.com/announcements/badge-management-for-ios/)

我在我的ios应用程序(parse.com sdk 1.7.2.2)上使用安装,推送和徽章,我几天前发现了一些内容,将徽章重置为0的代码,如博客文章中所述(旧版本http:// blog .parse.com /通知/徽章管理换IOS /)

// 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];
    }];
}

does not work anymore, everything is good (no Parse error) but the badge count stay to the old value in the database

不再工作,一切都很好(没有Parse错误)但徽章计数保持在数据库中的旧值

In a second time I tried the hard way, and it seems to work better for a moment:

在第二次我努力尝试,似乎暂时工作得更好:

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];
    }];
}

But this is not working,
any idea?

但这不起作用,任何想法?

3 个解决方案

#1


Heres my swift code and it works:

继承我的快速代码,它的工作原理:

    // 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
    }

#2


there seems to be a dependency between the installation.badge and the application.applicationIconBadgeNumber setters. Ensuring the installation is always set first seems to alleviate the issue.

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
            })
        }

#3


Using Parse (1.14.2), Xcode 8, and ios 10, adding:

使用Parse(1.14.2),Xcode 8和ios 10,添加:

    UIApplication.shared.applicationIconBadgeNumber = 0

inside the applicationDidBecomeActive method in the AppDelegate class will also reset the badge on the parse server to zero.

在AppDelegate类的applicationDidBecomeActive方法内部也会将解析服务器上的徽章重置为零。

#1


Heres my swift code and it works:

继承我的快速代码,它的工作原理:

    // 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
    }

#2


there seems to be a dependency between the installation.badge and the application.applicationIconBadgeNumber setters. Ensuring the installation is always set first seems to alleviate the issue.

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
            })
        }

#3


Using Parse (1.14.2), Xcode 8, and ios 10, adding:

使用Parse(1.14.2),Xcode 8和ios 10,添加:

    UIApplication.shared.applicationIconBadgeNumber = 0

inside the applicationDidBecomeActive method in the AppDelegate class will also reset the badge on the parse server to zero.

在AppDelegate类的applicationDidBecomeActive方法内部也会将解析服务器上的徽章重置为零。