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
Swiftui App Life Cycle Ios14 Where to Put Appdelegate Code
Uiscrollview Pauses Nstimer Until Scrolling Finishes
Uiwebview: Html5 Audio Pauses in iOS 6 When App Enters Background
#Warning: C-Style For Statement Is Deprecated and Will Be Removed in a Future Version of Swift
How to Handle Different Orientations in Ios
Always Pass Weak Reference of Self into Block in Arc
Remove or Uninstall Library Previously Added: Cocoapods
Uibutton Inside a View That Has a Uitapgesturerecognizer
How to Create a Guid/Uuid Using Ios
Getting a List of Files in a Directory With a Glob
Dismiss Keyboard by Touching Background of Uitableview
Detect When an iOS App Is Launched For the First Time
Xcode 6 Beta 2 Issue Exporting .Ipa: "Your Account Already Has a Valid iOS Distribution Certificate"
How to Put Buttons Over Uitableview Which Won't Scroll With Table in Ios