Update badge counter in Swift
Asked Answered
H

6

22

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?

Homogamy answered 24/4, 2015 at 9:3 Comment(1)
Can you please share your code and your Parse settings because it seems you resolved your issue and I have the same question but I can't fix it. I don't use Parse at this time but if you can success the update badge count I will use it. Thank you very much.Kura
H
22

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.

Hamadryad answered 24/4, 2015 at 9:9 Comment(11)
how can I send it from the server? How can I know how many notifications are unread?Homogamy
I looks like you're using parse.com right? If you, if you are sending a normal message (not using son) 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": "numberYouNeed" }Hamadryad
No, I use Pase.com. Can you tell me how to do it? Increment the value of badgeHomogamy
I just have done, check my last comment, i have also updated my answerHamadryad
no, I do not want to send it from the server. In the Parse.com documentation gameScore.incrementKey. Can I use it?Homogamy
This is the answer - just take a moment to read what Swinny89 has actually posted. Swinny89 - you may with to edit your answer with your commentMirabella
@Mirabella I do not want to send this number of badges from the server in JSON. I want to increment it after every notification if it did not read. If read - I reset it to 0Homogamy
Read the first line of my answer againHamadryad
your version just changed it to 1 and didn't increment it later. How can I increment it? I did send it as JSONHomogamy
Lets start form the beginning. How are you actually sending your push message? From parse.com under the push section?Hamadryad
Make sure message type is set to plain text, increment badge is yes, type your message and send it. If the app is not open then the badge will be updated for you. Upon opening your app, or whenever you need to, make sure you set the badge back to 0 using UIApplication.sharedApplication().applicationIconBadgeNumber = 0 and also using currentInstalltion.badge = 0 and then saving it in the backgroundHamadryad
O
25

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
Octaviaoctavian answered 24/4, 2015 at 9:32 Comment(4)
I paste it inside of didRegisterForRemoteNotificationsWithDeviceToken like: let notif = UIApplication.sharedApplication() notif.applicationIconBadgeNumber = ++badgesCount installation.saveInBackground() and nothing again =/Homogamy
if you don't make badge icon = 0 it will show all the timeOctaviaoctavian
please describe your application flow so we can adjust icon number. for example initially it is 0 when push notification it increment and after some task decrementOctaviaoctavian
deprecated in iOS 17Chatterton
H
22

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.

Hamadryad answered 24/4, 2015 at 9:9 Comment(11)
how can I send it from the server? How can I know how many notifications are unread?Homogamy
I looks like you're using parse.com right? If you, if you are sending a normal message (not using son) 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": "numberYouNeed" }Hamadryad
No, I use Pase.com. Can you tell me how to do it? Increment the value of badgeHomogamy
I just have done, check my last comment, i have also updated my answerHamadryad
no, I do not want to send it from the server. In the Parse.com documentation gameScore.incrementKey. Can I use it?Homogamy
This is the answer - just take a moment to read what Swinny89 has actually posted. Swinny89 - you may with to edit your answer with your commentMirabella
@Mirabella I do not want to send this number of badges from the server in JSON. I want to increment it after every notification if it did not read. If read - I reset it to 0Homogamy
Read the first line of my answer againHamadryad
your version just changed it to 1 and didn't increment it later. How can I increment it? I did send it as JSONHomogamy
Lets start form the beginning. How are you actually sending your push message? From parse.com under the push section?Hamadryad
Make sure message type is set to plain text, increment badge is yes, type your message and send it. If the app is not open then the badge will be updated for you. Upon opening your app, or whenever you need to, make sure you set the badge back to 0 using UIApplication.sharedApplication().applicationIconBadgeNumber = 0 and also using currentInstalltion.badge = 0 and then saving it in the backgroundHamadryad
A
8

In Swift 5, you can update de application's badge whenever you want, using this code:

UIApplication.shared.applicationIconBadgeNumber = 0 // YOUR NUMBER DESIRED
Avestan answered 30/10, 2019 at 14:29 Comment(0)
P
5

I have worked on similar scenario and the final solution I found to increment and reset the badge numbers.

Increment Badge number

  1. I always save the badge number count in the memory (NSUserDefaults)
  2. 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

  1. 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.
Poi answered 11/11, 2015 at 7:31 Comment(0)
W
5
UNUserNotificationCenter.current().setBadgeCount(x)

// deprecated UIApplication.shared.applicationIconBadgeNumber = x

Waverly answered 5/1 at 6:39 Comment(0)
C
0

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.
Constrictive answered 26/11, 2015 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.