Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do badge increment on iOS push notification

I'm currently getting badge from payload. But how can i update that value.

{"aps":
    {"alert":"Notification Hub test notification2",
     "badge":1,
     "sound":"Default"}
}

when i send this it is always shows 1 in the badge number but when i go inside app and exit it's getting updated.It is because of this,

func applicationDidBecomeActive(application: UIApplication) {
        UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    }

But when i send a new push notification it's again shows 1 as badge number in the icon. How should i do this, to my understand i should send updated badge when i send the payload. For that i have to deal with backend.

Is there any suggestion other than this to handle inside app?

like image 415
Anushka Madushan Avatar asked Jul 20 '16 09:07

Anushka Madushan


People also ask

What is iOS notification badge count?

The iOS badge count displays the number of unread notifications within your application, taking the form of a red circle in the upper-right hand corner of the app icon. In recent years, badging has come to be an effective means for re-engaging app users.

How do I get badge notifications on my 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. Step 4: If seeing the badge in your App Library is preferable, just switch the toggle back on for the full effect.

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


1 Answers

If your project has a notification extension, you can do a badge setting inside it.

First of all, enable App Groups and save your badge count from the app notification service extension in UserDefaults. We are using app groups because we need to clear the badge count from AppDelegate when opening the app. Also you can set value of badge as bestAttemptContent.badge from notification service extension

In notification service extension:

if let userDefaults = UserDefaults(suiteName: "group.com.bundle.appname") {
    
    let badgeCount = userDefaults.integer(forKey: "badgeCount")
    if badgeCount > 0 {
        userDefaults.set(badgeCount + 1, forKey: "badgeCount")
        bestAttemptContent.badge = badgeCount + 1 as NSNumber
    } else {
        
        userDefaults.set(1, forKey: "badgeCount")
        bestAttemptContent.badge = 1
    }
}

In AppDelegate you can clear badge...and set value for the badge as zero on userdefaults

if let userDefaults = UserDefaults(suiteName: "group.com.bundle.appname") {
    userDefaults.set(0, forKey:"badgeCount") // "change "badgecount" to be "badgeCount"
}
UIApplication.shared.applicationIconBadgeNumber = 0
like image 175
Chinju James Avatar answered Nov 15 '22 05:11

Chinju James