Advanced version of push notification Part 1— UNAuthorizationOptions

This article is the advanced version of the push notification series. This article is going to cover mostly the notification authorization options. Before start this article please go through the previous articles

  1. How to configure iOS app to receive push notification

2. How to generate authentication token/ push notification certificate to send push notification?

3. How to send push notification to real device without server configuration(for testing purposes)?

Let’s start

UNAuthorizationOptions

UNAuthorizationOptions is used when you are requesting authorization from the user for receiving the push notification. This section is going to cover what are the options available and how it’s used for.

Badge

When you request the push notification if you add this as an authorization option, apple will handle the badge for your notification(This value should be passed from the Notification payload), Badge indicate to the app how many notification received, but the number of counts should be pass from the payload app can’t handle by its self.

Notification with Badge

If you drag and drop the payload or send using the push notification app you will see the notification badge as above, But if you open the app and close the badge won’t get cleared. To clear the notification badge you should set the IconBudgeNumber as 0. UIApplication.shared.applicationIconBadgeNumber = 0

Clear badge number

Alert

This option will ask the users to display the push notifications. This is based on the system setup the alert can be displayed as a banner or alert.

Sound

This option will play the sound if the push notification arrived on the device. By default, the notification plays the default sound, with the payload help we can pass the sound name from the remote and play it from the device. If the given sound is not found in the app, it will play the default sound.

Custom sound file for push notification

Please note ⚠️

  1. The local notification sound length should be 30 sec or lesser than that
  2. If you play the longer notification sound users might get annoyed, So better think about some user experience when you have custom push notification sounds.

CarPlay

When we add this option in the push notification request permission, the push notification will be displayed in the Car Play also.

Critical Alert

This is another option available in the push notification request permission. To support the critical alert, you must apply for a special entitlement from apple to enable them. Because these critical alerts will be displayed to the user even if the user declined the permission and if the app is in the do not disturb mode. Usually, this is used in health care, home security-related and other public safety-related apps.

Provisional Authorization

Some people usually disabled the notification permission especially older people because there is not much information about what will be delivered and displayed to the user. So to work around, if we add these notification authorization options notification will automatically be added to the notification Center without asking for permission and there will be no sound or alert. Users who are looking at the notification Center will notice the notification and when they click the notification from that point they can allow or deny the notification permission.

Step 1

Add this logic in the register notification function.

Authorization options setup

Step 2

After did above code change run the app on real device, you should be able to get the device ID in the output console panel.

Step 3

By using the device ID, now open the push notification app and send one push. you should be able to see the notification in the notification center.

Notification in the Notification Center

Step 4

From this place itself, you should be able to accept or reject the push notification. By clicking the Turn Off button you can ignore the notification received.

If you click the Keep button there will be another alert will be presented which contains two options.

  1. If you select Immediate Deliver when you receive the push notification next time it will be delivered immediately.
  2. If you select the Deliver in Schedule Summary based on your device/app set up on the schedule summary the notification will be delivered to you.
Keep button options

Please note⚠️:

  1. This option is only available from iOS 12, so if your app is handling the previous iOS version you should handle that.
  2. These features can’t be tested in simulator, so after the code changes run the app on a real device you should be able to get the device token, by using that device token you can send the push notification using the push notification app.

Conclusion

This is the first part of the advanced notification with UNAuthorizationOptions, I’ll be writing another article for the notification payload. Keep in touch for the next article. I hope you guys learned new stuff from UNAuthorizationOptions what are those how it can be used what are the limitations. Let me know if you have any questions or suggestions to improve my article writing by adding comments in this article. Don’t forget to give me some claps 👏.

Check the latest code from this GitHub

Happy coding…..

See you in the next article.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Achsuthan Mahendran

Achsuthan Mahendran

iOS Developer, Web Developer, Flutter Developer. GitHub: achsuthan