With following code I get (2) in the badge icon immediately after app compiling:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.badge = 2
installation.saveInBackground()
}
I did try the next variant: Initialized a new var badgeCount = 0
and later:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
badgeCount++
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.badge = badgeCount
installation.saveInBackground()
}
But when I get new notifications it doesn't update to +1. Is anyone know how to fix it?
It won't update the badge number with this method unless the app is actually open. If you want to update the badge number upon receiving a notification then you need to set the Badge property of the json push notification to the desired number.
If you, if you are sending a normal message (not using json) there is a toggle to increment the badge number, just tick that. If you're using Json then use this:
{
"aps": {
"alert": "Test Push Notification",
"sound": "yourSound.aiff",
"Badge": "desiredNumber"
}
}
Please note, if you do not wish to send this from the server, you can also send it from one device to another utilising Parse's client push, go into your settings in the app on Parse.com and enable "client push", you can then send the above Json to another user's device.
Whenever code is compiled it shows the badge icon which is previously store in your app. If you don't set the badge icon = 0 in your app it will show the badge icon number in your app every time you compile it or enter in background state.
Now for your problem, use badge icon as
var badgeCount = 0
UIApplication.sharedApplication().applicationIconBadgeNumber = ++badgeCount
Also whenever you are done with your task make badge icon as 0 otherwise it will show a badge icon in your app
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
I have worked on similar scenario and the final solution I found to increment and reset the badge numbers.
Increment Badge number
- I always save the badge number count in the memory (
NSUserDefaults
)
- Every time i have to set the notification, I get the current badge number increment that and set that number on
.applicationIconBadgeNumber
and update the count in memory.
Reset Badge Number
- In my case, I have to reset all the badge count once the application is opened. So I have set
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
in didFinishLaunchingWithOptions
of AppDelegate
. Also I reset the count in the memory.
None of these answers are valid anymore.
You need to be looking at your Push code, not your AppDelegate
From the Parse docs:
badge: (iOS/OS X only)
the value indicated in the top right corner of the app icon.
This can be set to a value or to Increment in order to increment the current value by 1.