Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update badge counter in Swift

Tags:

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?

like image 797
Orkhan Alizade Avatar asked Apr 24 '15 09:04

Orkhan Alizade


People also ask

What is badge count?

Badge Count reflects all unread mail from your mail account(s), or just unread mail in your Focused Inbox.

Why are my app badges not showing numbers iPhone?

Check Settings app On your iPhone or iPad, leave Things and go to the Settings app. Tap Notifications. Scroll down to find Things and tap it. Enable the toggle for Badges (if it's already on, toggle it off and back on).

How do I get notification number for app icon on iPhone?

Step 1: Launch the Settings app on your iPhone or iPad. Step 2: Tap Home screen. Step 3: In the Notification badges section, toggle on the switch for Show in App Library.


1 Answers

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.

like image 182
Swinny89 Avatar answered Oct 31 '22 18:10

Swinny89