iOS 12: Custom Notification Groups
A long-awaited feature that groups all of the notifications
In the last WWDC, along with many other things Apple announced Grouped Notifications as part of the new features for iOS 12.
Today I will give you the a quick intro about how to have your own Custom Notification Groups, this might be very handy specially if you have sort like a messaging app or social network app, but first:
Disclaimer: This tutorial was made using Xcode 10 beta 6, so some things might change once it is released officially.
Grouped Notifications
Grouping the notifications your app sends helps people get more information at a glance and manage multiple notifications at once.
In iOS 12 there are 3 settings for grouped notifications:
- Automatic: This is the default setting, which will allow you to have “smart” notification groups for each app. Based on title, or content, or what it say, or the sender in case of the Mail app.
- By App: Basically all your notifications will be stacked in a single group by app, no differentiation by title, content or sender.
- Off: In case you want to stick to the current way where each notification for each app will be show individually.
Schedule Notifications
For the sake of the article I will be using UNMutableNotificationContent
so I can trigger Local Notifications in the simulator.
First you need to have user authorization to send him notifications. You can do that by add this code to the viewDidLoad
method of your demo project.
NOTE: All this is just for the sake of the tutorial you should handle the request authorization in a better place than viewDidLoad.
After have the notification permissions we will be working using the following code snippet:
The previous method creates a set of UNMutableNotificationContent
that will be triggered 5 seconds later. This with the purpose of have enough time to lock the screen and see them in action. I attached that code to a IBAction
in my demo project and since iOS 12 has Automatic grouping by default it looks something like this:
Custom Notification Groups
Under some circumstances the Automatic Grouping might not be that good to your app, maybe you are in the need to create custom groups based on any metric or variable inside your logic.
Going back to our demo project, lets assume I need to group all the notifications that comes from “my wife” and the ones that comes from “my son”. In order to do that, we will update our notificationContent
object by using :
threadIdentifier
: The unique identifier for the thread or conversation related to this notification request. It will be used to visually group notifications together.
And for extra information:
summaryArgument
: The argument to be inserted in the summary for this notification.
Now our scheduleGroupedNotifications
method should look something like this:
And on the simulator it will looks something like this:
Notice how we have two groups defined with the threadIdentifier
and with summaryArgument
we add “your wife” or “your son” accordingly to the summary detail.
And with that you will be able to have Custom Notification Groups.
In case you need it you can get the demo project here.
Sources
Words, once they are printed, have a life of their own.
― Carol Burnett
Thanks for reading and hope you find this article helpful. As usual any feedback or comment is welcome. And if you want to waste some of your time, follow me on twitter @guerrix