This text mostly focuses on the local notifications and what we can do in UIViewController in Notification Content Extension.
Notifications in iOS 10 got quite a lot of improvements. The first one is a higher contrast with the notification view on a Lock Screen, so it stands out more. The second one is a total customization of notification presentation.
As seen in the picture above, the notifications stand out more and are attention-grabbing.
We can put custom content in the notification: music, video, images, custom drawing and animations.
When a user is unlocking his device, the first thing a user will see when he taps on the Touch ID button are notifications that are waiting for him.
Since the lock screen unlocking procedure has also been changed, the notifications have a greater chance of getting attention from a user. To unlock a phone, the user needs to tap twice and it is during this process that notifications get a small window of opportunity for presentation.
When the phone is locked, tapping the Home button reveals Lock Screen with pending notifications for user.
Tapping on the notification on the lock screen will open associated app for that notification. Swiping the notification to the left reveals View and Clear buttons. The View button is available only if device does not support 3d Touch.
On devices with 3d Touch, the user can get preview of the content by force touching the notification, which is either on the Lock Screen or Springboard. For example, the user receives a notification about new message for him. User can Force Touch notification and open the message view. The user can then type reply directly on the lock screen, instead of opening the app.
When a notification appear on Springboard, user can swipe down on notification view and get content extension presented to him. That is possible if the app has Notification Content Extension implemented about that notification.
In addition, developers can add more than one extension to their app. They just need to create new target in Xcode project and assign a category identifier in extension’s Xcode target Info.plist file.
On the time of writing, no iPad devices has Force Touch feature. Some iPhone models in production do not have Force Touch feature. On these devices, user can reveal notification by swiping.
Developers can add actions to notifications. Actions are visible when user expand the notification.
We don’t need to have Notification Content Extension for this feature.
Developers can add actions to the notification when configuring the category object:
1let cool = UNNotificationAction.init(identifier: "CoolAction", title: "Cool", options: [.foreground]) 2let approve = UNNotificationAction.init(identifier: "ApproveAction", title: "Approve", options: [.foreground]) 3let later = UNNotificationAction.init(identifier: "LaterAction", title: "Later", options: [.foreground]) 4let like = UNNotificationAction.init(identifier: "LikeAction", title: "Like", options: [.foreground]) 5let category = UNNotificationCategory.init(identifier: "CategoryId", actions: [cool, approve, later, like], intentIdentifiers: , options: ) 6
Note that we need category identifier later, when configuring the notification content object.
Notification category needs to be created and registered with
UNUserNotificationCenter early in the application lifecycle, preferably in app delegate.
Every notification can have several actions assigned to it. The actions will appear in an ordered fashion. If we put more notification actions that can fit in the screen real estate, user will not be able to use them.
Actions are the only way a developer can enable user to interact with the notification, because adding Gesture Recognizers or UIControl objects won’t work. UIViewController that is shown when notification is expanded does not process touch input. The default behaviour for tapping the notification view is launching the app.
Notification attachments are a new way of customising notifications. This is a convenient and easy way to add content to notifications without making an extension. With the help of
UNNotificationAttachement class we can attach media files to the notification. By using the options dictionary argument we can also customise the thumbnail image.
We can add three types of attachments to notifications:
1. Image attachment:
2. Video attachment:
3. Music attachment:
When a user interacts with notification, media which is inside attachments are shown or playback controls are presented, depending on media type.
For example, if we send a birthday notification to user we can send image of contact whose birthday it is.
All attachments are created by creating
UNNotificationAttachment object with path to media resource and attaching that object to notification content object.
1let url: URL? = Bundle.main.url(forResource: "sound", withExtension: "mp4") 2let attachment = try? UNNotificationAttachment.init(identifier: "AttachmentId", url: url!, options: [UNNotificationAttachmentOptionsTypeHintKey : "mp4"]) 3
We cannot get text from the user by an UITextView or UITextField. Putting those objects in our UIViewController will just display them, but the user won’t be able to interact with them. The keyboard will not show on touch gesture, instead notification content extension view will process tap gesture as interacting via the notification view. Depending on whether notification occurred in our app or on Springboard, tapping will close the notification view and then show the app.
ViewController with moving CALayers
The Notification Content Extension contains full UIViewController for absolute customization. As mentioned, there is no way to process touches on that screen. But nothing stops developer from putting some pictures to move around and paint some UIView’s using Core Graphics.
Developers can use Core Animation frameworks in their notifications. In this extension, we have several CALayer objects moving around:
Drawing is also possible, here we have UIView with the overriden drawRect method:
As we can see, many things are possible with new notifications.
Ok, so we have powerful notifications. How are we going to write code for them and debug it?
The developer can debug the notification content extension code by putting breakpoints in his project. When trying to debug notifications, breakpoints in the notification target won’t work unless target is changed. In Xcode, target must be switched to point to the notification content extension target.
After that, when Run button is clicked, Xcode will ask to select an app:
Breakpoints that are set in the notification content extension will now be hit and debugging is possible.
Conclusion: we can use full UIViewController capabilities, along with autolayout, to produce beautiful and inviting notifications.
Grab the project here .
Dein Job bei codecentric?
More articles in this subject area\n
Discover exciting further topics and let the codecentric world inspire you.