Skip to main content
Quick Reference for AI Agents & Developers
  • Requires: UNNotificationServiceExtension target in your app
  • Purpose: Update badge count when push notification received
  • Implementation: Override didReceive(_:withContentHandler:) in extension
  • Related: Push Notifications · Prepare for Background
This guide helps you to set up an incremented badge for the app icon using Notification Service Extension.

UNNotificationServiceExtension

This service grabs the data from the push notification payload, the user can modify its content and display the customized data on to the push notification. In our case, we are modifying the data of the push notification and incrementing the badge count when a new push notification is received. Let’s begin to implement an incremented badge count for your app.

Step 1: Add UNNotificationServiceExtension inside the app.

  1. Click on File —> New —> Targets —> Application Extension —> Notification Service Extension.
  1. Add Product Name and click on Finish.

Step 2: Setup App Groups.

1 . Click on Project —> Targets —> Your app Target —> Signing & Capabilities —> [+] —> App Groups.
  1. In App Groups, click on [+] —> Add a new container —> Enter group name —> OK.
Kindly, create a group name using the combination of ‘group’ and ‘App’s bundle identifier’ i.e group.com.yourApp.bundleId.
  1. Make sure you’ve selected the app group which you’ve created earlier. If it is selected then it will look like a below-mentioned image.
  1. Click on Project —> Targets —> Notification Service Extension Target —> Signing & Capabilities —> [+] —> App Groups.
  1. Select the same App Group which you’ve created in Your app Target.

Step 3: Setup user suit for storing badge count.

  1. Open AppDelegate.swift and add below code in applicationWillEnterForeground(_ application: UIApplication).
func applicationWillEnterForeground(_ application: UIApplication) {

    UserDefaults(suiteName: "group.com.yourApp.bundleId")?.set(1, forKey: "count") 
    UIApplication.shared.applicationIconBadgeNumber = 0

}

Step 4: Setup Notification service extension to increment badge count.

  1. Open NotificationService.swift and replace the below code in it.
import UserNotifications

class NotificationService: UNNotificationServiceExtension {
    
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    let defaults = UserDefaults(suiteName: "group.com.yourApp.bundleId")
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        var count: Int = defaults?.value(forKey: "count") as! Int
        if let bestAttemptContent = bestAttemptContent {
            bestAttemptContent.title = "\\(bestAttemptContent.title) "
            bestAttemptContent.body = "\\(bestAttemptContent.body) "
            bestAttemptContent.badge = count as? NSNumber
            count = count + 1
            defaults?.set(count, forKey: "count")
            contentHandler(bestAttemptContent)
        }
    }
    
    override func serviceExtensionTimeWillExpire() {
     
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}
UserDefaults Storage (App Groups):
ParameterTypeDescription
suiteNameStringApp Group identifier. Example: "group.com.yourApp.bundleId"
keyStringStorage key. Example: "count"
valueIntCurrent badge count. Example: 5
Notification Content Before Modification:
ParameterTypeDescription
titleStringOriginal notification title. Example: "John Doe"
bodyStringOriginal notification body. Example: "Hello, how are you?"
badgeNSNumber?Badge count (nil before modification). Example: nil
Notification Content After Modification:
ParameterTypeDescription
titleStringModified title. Example: "John Doe "
bodyStringModified body. Example: "Hello, how are you? "
badgeNSNumberBadge count set. Example: 5
Badge Increment Sequence:
StepActionValue
1Read count from UserDefaultscount = 1 (initial)
2Set badge on notificationbestAttemptContent.badge = 1
3Increment countcount = 2
4Save to UserDefaultsdefaults.set(2, forKey: "count")
5Next notification arrivesbadge = 2, then save 3
applicationWillEnterForeground Action:
ParameterTypeDescription
UserDefaults.setIntReset count to 1. Example: defaults.set(1, forKey: "count")
applicationIconBadgeNumberIntClear badge from app icon. Example: 0
Effect:
ActionDescription
Badge clearedApp icon badge removed
Count resetNext notification will show badge = 1

Refer Sample App

We have implemented it in our push notification sample app. You can download the sample app. Download Push Notification Sample App Or else view it on Githhub.