How to reset/clear the iOS App's badge?

RemoteNotification.applicationIconBadgeNumber is read only, which means I cannot change the badge locally.

I know I gotta use the server to track the badge number. But the situation is, once I received the push notification and activate the app, I upload the data, telling the server “I’ve got your notification”, so that the badge number on the server’s end will reset to 0. But the problem is I cannot change the badge num locally, so the App’s badge number didn’t change.

In order to do that, I gotta let the server send an extra msg after he receives the “I’ve got your notification” msg, telling the App “okay. reset the badge num now”. What an extra work!

Do I have any way to modify/reset/clear the application’s badge number on App’s end in Unity3d?

Additionally, even I send the extra msg from my server, telling the App to reset the badge, the only way to do that is to send a push notification, setting the badge's number in the payload. That sounds ridiculous! Because that means I gotta use 2 push notifications, one for the real deal, and the other for clearing the badge?? That's impossible, so it gotta be some other way, even without extra plugins.

could you use LOCAL notifications ?????????? to do this ? I know that at Prime31 that is the "Etc Two" package.

awesome you could do that at the XCode level. good one

5 Answers

5

OneWhoMikes’s answer works for me with any badge number I want, except for 0.
To clear the badge, use applicationIconBadgeNumber = -1.

Source: http://forum.unity3d.com/threads/127016-Using-the-new-notification-system?p=921134&viewfull=1#post921134

Thanks for adding this comment! The -1 is the correct answer and works perfectly.

Thanks! Works fine with -1.

Thanks Fattie. I’m now using the Obj-C in Xcode to set the badge number. Problem solved. But I’m still curious about how Unity3D’s gonna set that value. And I’m wondering what’s the purpose of make the badge number a read-only in Unity

Something like the following will work from within Unity, no need for Obj-C code:

LocalNotification setCountNotif = new LocalNotification();
setCountNotif.fireDate = System.DateTime.Now;
setCountNotif.applicationIconBadgeNumber = 0;
setCountNotif.hasAction = false;
NotificationServices.ScheduleLocalNotification( setCountNotif );

I’m still struggling with setting the badge through Unity code. If you just want to get rid of the badge whenever the app starts or resumes then add this you your plugins folder.

//
//  ClearBadgeHelper.m
//  Unity-iPhone
//
//  Created by Antony Blackett

#import <Foundation/Foundation.h>

@interface ClearBadgeHelper : NSObject
+ (void) clearBadge:(NSNotification *)notification;
@end

@implementation ClearBadgeHelper

+ (void) clearBadge:(NSNotification *)notification
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

+ (void)load
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearBadge:) name:UIApplicationDidFinishLaunchingNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearBadge:) name:UIApplicationDidBecomeActiveNotification object:nil];
}

@end

It’s 2017 now. Unity provides following functions to clear notifications and badge numbers.

NotificationServices.ClearLocalNotifications()
NotificationServices.ClearRemoteNotifications()