From the course: iOS and watchOS App Development: Notifications

Add an image in content extension

From the course: iOS and watchOS App Development: Notifications

Start my 1-month free trial

Add an image in content extension

- [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.

Contents