Payloads are Your Content:

Much of what we just did replaces the trigger in a local notification. Content for a notification is found in the payload. Going back to the testing platform, you’ll find this:

{"aps":
{
"alert":"Enter your message","badge":1,"sound":"default"
}
}

Ideally your JSON file should look like this. You only have 4K for your payload so wasting it on spaces is frowned on. When sending payloads avoid whites-pace. However, they are hard to read this way. It looks better like this:

{
"aps":{
"alert":"Enter your message",
"badge":1,
"sound":"default"
}
}

The aps is a JSON dictionary with entries that describe your content. The alert entry is can be a string like it is here, or a dictionary describing the content of the alert that shows on your device. The badge give the number to show on the badge icon. The sound plays the default sound. You can modify this payload to change the content displayed in the alert. As the alert can be both a dictionary or a string you can add more to it. Change the payload to this:

{
"aps":{
"alert":{
"title":"Push Pizza Co.",
"body":"Your pizza is ready!"
},
"badge":42,
"sound":"default"
}
}

This will add a title and a message about your pizza being ready. It will also change the badge to 42.

{"aps":{"alert":{"title":"Push Pizza Co.","body":"Your pizza is ready!"},"badge":42,"sound":"default"}}

The notification appears with the title and body. The badge appears with the number 42.

However, you can also change it while your app is active. You will need permission from the user by registering the UserNotificationType. Once you get permission, you can change it to whatever number you wish.

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil
))
application.applicationIconBadgeNumber = 5

--

--

Mr.Javed Multani

Software Engineer | Certified ScrumMaster® (CSM) | UX Researcher | Youtuber | Tech Writer