How to Display Image in iOS Push Notification

Is it possible to display an image inside the title text in an iOS Push Notification?

No, it's not possible to include images in notifications like that. You must include them as media attachments (which will appear at the right side of the notification), more information about that here.

Alternatively, you can use an emoji in the title of the notification.

We want to show Image with Push notification

This two links may help:

rich notifications

iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C

Images in iOS push notification

This line:

if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {

Is looking for an attachment-url value as a child of a data object in the userInfo dictionary.
It is looking for this:

{ 
"aps" : {
...
},
"data" : {
"attachment-url" : "some url"
}
}

But the payload in your question is this:

{
"aps":{
"sound":"default",
"alert": {
"title":"iOS",
"body":"Hello Dude...."
},
"mutable-content": 1
},
"CustomData": {
"mType":"alert",
"m":"Hello Dude...."
},
"Attachement-url":"https://pusher.com/static_logos/320x320.png"
}

The "data" section does not exist, and the attachment-url key does not exist.

Change your Swift code to match what is in the payload and you should be able to get the image URL and download it.

You will have a big problem if you receive a notification that does not have the attachment URL key or the attachment URL is not a properly formed URL. In those cases your if let will not be entered and the contentHandler will not be called! This will not just cause the service extension to lock up, but it will prevent any notification that does not have the attachment URL from being delivered! Add an else that calls the contentHandler to fix this.

Once you have it downloaded though there is another problem. iOS will need to know what kind of data you are putting in the attachment. The attachment options dictionary allows you to include type information about the attachment. Get the MIME Type of the downloaded file and create a Uniform Type Identifier from that. The Uniform Type Identifier string can then be used in the options dictionary.

I cover all of this in depth in the iOS Notifications book. The sample chapter available now deals with adding images to notifications.



Related Topics



Leave a reply



Submit