UserNotification Example III – Notification Extention

This example share a lot of common from UserNotification Example I, it will use time trigger because it’s the simplest way to test notification. Besides, we will use delegate to make the notification appeared inside in-app.

Adding the Notification Content Extension

1. Generate Notification Extension Target

Named it as ‘Notification Extension’. Leave everything else for default.

We will get a folder with content like this.

We have a NotificationViewController.swift, a storyboard and Info.plist.

2. Edit Info.plist

And to make sure that the Notification Content Extension get showed is at the Info.plist, make sure your notification’s category is the same with the want that we will declared in Info.plist.

Open the Info.plist file, find the UNNotificationExtensionCategory, inside NSExtentionAttributes, burried inside NSExtension. Name it as “notification-category”.

Also, when you have the notification extension, you may end up using the same attibutes like title, subtitle, and body. Why not hide the default view which showing event we have the notification extension. To hide it add this key “UNNotificationExtensionDefaultContentHidden” and set the value as “YES“, inside NSExtensionAttributes’s dictionary.

We also want to make the initial content show up not too big, so we can set this key “UNNotificationExtensionInitialContentSizeRatio” to “0.3” because we will have smaller content footprint.

3. Storyboard

Simple editing to the default storyboard, create 3 labels and put in into stacked. Pin the stack view around the superview. Learn some tips on Auto Layout.

Set the stack view’s distribution as ‘Fill Proportionally’.

4. NotificationViewController

We need to properly link the labels to the view controller as IBOutlet like so.

Open the Info.plist file, find the UNNotificationExtensionCategory, inside NSExtentionAttributes, burried inside NSExtension. Name it as “notification-category”.

Also, when you have the notification extension, you may end up using the same attibutes like title, subtitle, and body. Why not hide the default view which showing event we have the notification extension. To hide it add this key “UNNotificationExtensionDefaultContentHidden” and set the value as “YES“, inside NSExtensionAttributes’s dictionary.

We also want to make the initial content show up not too big, so we can set this key “UNNotificationExtensionInitialContentSizeRatio” to “0.3” because we will have smaller content footprint.

3. Storyboard

Simple editing to the default storyboard, create 3 labels and put in into stacked. Pin the stack view around the superview. Learn some tips on Auto Layout.

Set the stack view’s distribution as ‘Fill Proportionally’.

4. NotificationViewController

We need to properly link the labels to the view controller as IBOutlet like so.