Join Steven Lipton for an in-depth discussion in this video Calendar notification triggers, part of iOS and watchOS App Development: Notifications.
- [Instructor] The second type of notification is a calendar notification. If you have a date and time you want to launch the notification, this is the one to use. Scroll to schedule pizza method up above, and we're going to add some stuff here. We're going to use a UN calendar notification trigger, and I'm going to put it right after where it says time to make a pizza, and I'm going to do a let trigger equal UN calendar notification trigger, and I'm going to use the date matching repeats version of this, and for the date components, I'm going to change this to date components.
Then I'm going to move over here to repeats, and we're going to do that false again, like so. Now this should give me an error, because date components wasn't actually defined yet. This date matching parameter, is a date as a date components object. For now, we've set it to date components. So this date components, is a date components object. Date components breaks a date into a series of components you can use to set a date.
For a quick review of date components, you can check out an iOS Development Tips Weekly in the library on the subject. You'll most likely convert a date to a date component like this, which I'm going to stick above the trigger, and what you do, is I'm going to do var date components equals, and this is an object calendar, go to the current calendar, and you get the current calendars date components.
So you're getting, in all intents and purposes, I'm going to get the hour, minute, and seconds from the current date. So this is the current calendar, I'm going to check its date components, it's going to ask me for a set of date components I'm interested in. I'll put a brace so I can put the set in here, and I'm going to go for hour, minute, and second, so I have a time, and I'm going to base that on the current date and time, which I can get from just instantiating a date object right into here.
So what that'll do, is this'll make this be this value here. Now, I'm running into a little bit of a problem here. I would usually put some date in here, so you know, you say July fourth or something like that, or, I don't know, September 31st or something like that for a date that you'd put in here, so you could schedule something. And it's really meant for that kind of calendar scheduling, however, with a course like this, it's hard to set up a date that would work for everybody so that you could see it work, and, for that reason, I'm going to do something that will allow this date components just start a time 15 seconds into the future.
Very similar to what the time intervals really doing, and that is date components dot second equals date components dot second, and you'll notice that is an optional, so I'm going to have to unwrap it, and then its integer, so I'm going to use 15 seconds here, like that. And that takes care of that, now we just get the warning that I haven't used trigger yet, and that sets up the second of the time based triggers.
They're not that difficult to use, once you set them, they go off on their own. Calendar components are your way of setting times in the future, that don't make sense setting them with time intervals. The last trigger is a location trigger, and that uses a coordinate and range around a coordinate. Since that one requires a good understanding of core location, I'm going to skip it in this course.
- Requesting user permission
- Static vs. dynamic notification content
- Sending local notifications
- Updating and removing notifications
- Creating custom notifications
- Adding categories and actions to notifications
- Pushing notifications
- Building push notification payloads: alerts and actions
- Adding service and content extensions
- Attaching rich media: images, audio, and video
- Creating watchOS notifications
Skill Level Intermediate
Apple watchOS 4 App Development Essential Trainingwith Steven Lipton3h 10m Intermediate
1. User Permissions
2. Local Notifications
3. Notification Management
4. Interacting with Notifications
5. Push Notifications
6. Push Notification Payloads
7. The Service Extensions
8. The Content Extension
9. Rich Media in Notifications
10. Apple Watch Notifications
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.