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.
- Click on
File
—>New
—>Targets
—>Application Extension
—>Notification Service Extension
.

- Add
Product Name
and click onFinish
.

Step 2: Setup App Groups.
1 . Click onProject
—> Targets
—> Your app Target
—> Signing & Capabilities
—> [+]
—> App Groups
.

- 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
.
- 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.
- Click on
Project
—>Targets
—>Notification Service Extension Target
—>Signing & Capabilities
—> [+] —>App Groups
.

- Select the same App Group which you’ve created in
Your app Target
.

Step 3: Setup user suit for storing badge count.
- Open
AppDelegate.swift
and add below code inapplicationWillEnterForeground(_ application: UIApplication)
.
Step 4: Setup Notification service extension to increment badge count.
- Open
NotificationService.swift
and replace the below code in it.