简体   繁体   中英

Installation and badge issue with Parse.com and ios

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

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

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?

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
    }

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.

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

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

    UIApplication.shared.applicationIconBadgeNumber = 0

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

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