Join Steven Lipton for an in-depth discussion in this video Add an image in content extension, part of iOS and watchOS App Development: Notifications.
- [Instructor] A big takeaway about loading images into push notifications, is you're better off doing it in the content extension. There's several ways to get images into a notification but the simplest is in the content extension with internal images. There's a few things you need to know to use the content extension and images though. But before we do anything, let's turn the content extension back on. So let's go down the info.p list and change from snoozer.category to snooze.category.
This case I'll be using the assets folder in the Huli Pizza Notifications for the extension as well. So let's go to assets.xcassets, open up the attributes, and I want to make sure that the service and content extension are both checked for target membership. If you stored your image in the assets folder, you simply have to assign the image to image view. So the simplest thing we can do, of course, is just go to the interface storyboard, and for the image view, click the image.
I'm going to go ahead and click the Huli logo 300 here, and that is pretty much all I need to do for a simple image. This will launch very simply, I'm going to go change back to the iPhone XR for this in the simulator and I'm going to build and run, I'll schedule a pizza. And come over here to make sure it's there, there it is. It's got the attachment still, but when you load it back up, you'll see our image from the content extension.
Go ahead and close that, and close the app. The second way you can do this, is you can do it by code. So let's head back to the notification view controller.swift and I'm going to add it to the code. And I do it just like I would do for any other image. So I'm going to do back down here, and let's go to did receive. All the way down here and we've got, just like we did with the content body and content subtitle, we can do the same thing, content image view.image and I'll just use a UI image.
UI image named, since I made it a target, I can actually use the smaller Huli logo. Huli logo underscore 20 image in attributes, and I can run this. Schedule a pizza, see that it's there, slide it down, and there we have our smaller image of the pizza.
If you can, you will solve a lot of time out problems by having the images preloaded in your content extension, or your app.
- 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.