Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Schedule a local notification by configuring the content, trigger and composing a request object.
-
0:00
Back in Xcode within our ViewController here is where we're going to configure
-
0:05
a local notification.
-
0:07
So we'll create a function called scheduleNotification,
-
0:13
which we will call from our viewDidLoad methods.
-
0:18
And then let's implement the scheduleNotification.
-
0:26
We're going to delete that did receive memory warning.
-
0:31
We don't need that template method.
-
0:35
If you recall from the slides, we need three objects to create a notification.
-
0:39
The first being the content.
-
0:42
Let's create our content object first.
-
0:45
We will use the class name UNMutableNotificationContent.
-
0:50
So the same let content =
-
0:53
UNMutableNotificationContent and
-
1:00
as you can see I don't have autocomplete because I forgot to import
-
1:05
the UserNotification module.
-
1:11
Okay, if we command click on this class,
-
1:14
we can look at its definition and see what all do we have available.
-
1:21
So we have the badge, we have a body, we have
-
1:26
an image, we have sound, we have subtitle and we have title.
-
1:33
So let's start with the basics.
-
1:36
Let's start with the title, subtitle, and body.
-
1:40
So it's a content.title = 10
-
1:45
second notification demo and
-
1:49
then content.subtitle = From Treehouse and
-
1:56
finally, content.body = Notification
-
2:02
after 10 seconds- Your alert is ready!
-
2:08
Now that we have our content,
-
2:13
we need a way to trigger our notification.
-
2:21
We will use a time based trigger.
-
2:24
Remember we have either time based, calendar based, or
-
2:28
location based triggers.
-
2:30
So for this example, we're going to use the time base trigger.
-
2:35
So let say let trigger = UNTimeIntervalNotificationTrigger.
-
2:42
And this takes in two parameters.
-
2:44
The time in seconds.
-
2:47
So as you guessed from the title and
-
2:49
the body this is going to be a 10 second trigger notification.
-
2:54
And whether it repeats or not will say false because we don't want this
-
2:58
notification to keep repeating every 10 seconds.
-
3:01
That would be highly annoying.
-
3:07
So finally we need to create a request.
-
3:10
The request composes a notification request object which can then be
-
3:14
added to the system's default notification center of the notification queue.
-
3:21
So let's say, let request = UNNotificationRequest,
-
3:29
which takes in three parameters.
-
3:35
Now the first parameter is an identifier for our notification.
-
3:40
You see this goes into a notification queue.
-
3:42
And if we wanted to later retrieve that from the queue before it has been
-
3:47
triggered we can retrieve it and either delete it or update it.
-
3:52
So it's important that we give it a unique identifier.
-
3:56
I'll just call this 10.second.notification.
-
4:02
And the content is our content object and the trigger is our trigger.
-
4:08
And finally we will add request to our current notification center.
-
4:13
So as we had done in our app delegate we will access the current
-
4:18
notification center and then we can simply add to it.
-
4:23
So we add with two parameters,our request
-
4:30
and we can specify completion handler or
-
4:33
we can simply say no if you don't want a completion handler.
-
4:37
So that's it.
-
4:38
We have created the content trigger in requests and then we have added
-
4:42
that request to our default or current notification center and this should
-
4:47
be enough to schedule a notification which will display an alert in 10 seconds.
-
4:53
So let's go in and run our application.
-
4:56
So will first let our app launch in the simulator and
-
5:02
then will click on hardware and home so
-
5:06
that we can see the notification alert and there it is.
-
5:10
It say's Ten second notification demo from tree house.
-
5:15
So congratulations we've generated our first notification.
-
5:20
Next let's add our badge app icon when this notification is fired.
-
5:25
Oftentimes you want to grab a user's attention by displaying a badge app icon.
-
5:31
For example the number of unread emails or messages.
-
5:36
So going back to our code within our content.
-
5:40
So as you must have noticed that the content object has a badge property,
-
5:45
which we're going to set to 1.
-
5:48
Course you can assign any number to it, but we're going to assign it a 1.
-
5:51
And then run our application in the simulator.
-
5:56
And as soon as our notification is displayed, you will notice that a badge
-
6:00
with the number one is displayed on top of our app icon.
-
6:03
And here you go as you can see that red one or
-
6:08
that one with the circular red on top of the app icon.
-
6:13
So great. This is how you badge an app icon.
-
6:17
So next we're going to set the sound property on our content object.
-
6:22
So if we want to have a standard sound,
-
6:28
we can simply do a content.sound = UNNotification.
-
6:37
NotificationSound.default().
-
6:41
So this is going to be a default system sound for our notification.
-
6:47
But if you want something custom, we can do that as well.
-
6:51
So in our case, we're gonna add a custom sound.
-
6:54
So for that, I need to add this file to our project first.
-
6:58
So I'm gonna drag this gong.aif, aif stands for audio interchange format.
-
7:06
So make sure that it's selected copy items if needed and add to targets.
-
7:11
So I'm gonna select the target here which is the NotificationsDemo and
-
7:15
then hit Finish.
-
7:19
So instead of using the default sound, we're going to give it a name of a file.
-
7:28
So in our case it's called gong.aif.
-
7:33
So now when our application runs, and our notification is displayed,
-
7:37
it's going to have this nice little gong sound upon displaying.
-
7:44
I'll notice that the badge does not disappear.
-
7:47
There's that beautiful gong sound.
-
7:56
And notice that the badge does not disappear from previously if you want
-
7:59
the badge to disappear, then we would have to set it to 0 and
-
8:05
that will make the badge disappear.
-
8:07
Though you can updated to a different number.
-
8:10
I'm just gonna set it back to 1.
-
8:12
So finally we can now create rich notifications by displaying either video,
-
8:17
images or audio within them.
-
8:19
So let's add an image to our notification.
-
8:23
So I'm gonna drag this image to our project first.
-
8:30
Once again, make sure that the copy items if needed is checked, and
-
8:34
your target is checked, and then hit finish.
-
8:37
So first we need the URL to that image within our application bundle.
-
8:44
So I'm gonna say let imageURL = Bundle.main.url and
-
8:52
we're going to select forResource withExtension.
-
9:00
So this is treehouse and the extension here is jpg.
-
9:09
So from there we need to create an attachment object using
-
9:12
the UNNotification attachment class.
-
9:16
So I'll say let the attachment
-
9:21
equal UNNotification.
-
9:27
And here is the designated initializer.
-
9:30
So the first is the identifier of the attachment next is a URL and
-
9:35
then finally you have options.
-
9:40
So the identifier we'll simply just called this treehouse.jpg.
-
9:45
The URL is the imageURL.
-
9:48
And options, we're just going to specify nil.
-
9:52
But if we look at the definition for this method,
-
9:56
so the compiler is complaining here.
-
9:59
And I think we just need an exclamation to implicitly unwrap this imageURL optional.
-
10:07
We have yet another error.
-
10:12
So that's not the error which the other it says can call throw right.
-
10:16
So this notification attachment throws in error.
-
10:21
And we are not catching it or you're not even specifying a try.
-
10:24
So we just put a try bang over there.
-
10:29
And now we have an attachment object.
-
10:32
So what I wanted to show you as if you option click,
-
10:36
you will get all the options.
-
10:38
So it says Attachment Attributes.
-
10:41
You can see that in the documentation and you have different kinds of attributes.
-
10:46
Like one of the things is how do you want your image thumbnail to be clipped?
-
10:52
You can specify the clipping record over here or
-
10:54
if you want the thumbnail to be hidden or if you want HindKey.
-
11:01
So all of these options you can specify.
-
11:05
So finally, we need to attach this to our content.
-
11:08
So we'll say content.attachments = [attachment].
-
11:13
So you can have multiple attachments and
-
11:16
in our case we simply have this one attachment.
-
11:22
Okay, let's run our application and see how it displays the image.
-
11:30
Command shift H to go home and our alert will soon be displayed.
-
11:38
There it is.
-
11:39
And if you pull down on this alert,
-
11:41
what it does it shows you an expanded view of what the image looks like.
-
11:46
So the user can do that if they're curious to see what the image looks like.
-
11:50
All right, and
-
11:51
then we can just swipe up or just hit on this x to get rid off the alert.
-
11:58
So we've covered all the basics of creating the content for a notification.
-
12:02
So you've seen how you can create a basic notification with titles,
-
12:06
subtitle and body.
-
12:08
Then you saw that you can add a badge to the app icon.
-
12:12
You can add custom sounds are used default system sounds.
-
12:17
And finally we added an attachment in our case we added an image attachment but
-
12:22
similarly you can add a video or audio attachment if you wanted to.
-
12:27
So next stop, what happens once the user receives the notification and taps on it.
You need to sign up for Treehouse in order to download course files.
Sign up