Images in iOS push notification

2020-02-07 10:26发布

问题:

I am trying to send images in push notifications I have made the notifications registrations in app delegate and apns device token is generating properly. ALso I have coded in service ext as follows:

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

// Get the custom data from the notification payload
if let notificationData = request.content.userInfo["data"] as? [String: String] {
    // Grab the attachment
    if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
        // Download the attachment
        URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
            if let location = location {
                // Move temporary file to remove .tmp extension
                let tmpDirectory = NSTemporaryDirectory()
                let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
                let tmpUrl = URL(string: tmpFile)!
                try! FileManager.default.moveItem(at: location, to: tmpUrl)

                // Add the attachment to the notification content
                if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
                    self.bestAttemptContent?.attachments = [attachment]
                }
            }
            // Serve the notification content
            self.contentHandler!(self.bestAttemptContent!)
            }.resume()
    }
}
}
}

. And the payload in json is as follows

{
    "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"
} 

I am receiving the title and message but image is not coming. Please guide how to get image in push notifications

回答1:

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.



回答2:

For Swift, If you want you can try with this framework

Also Add "content-available":1 in your aps

OR you can try downloading like this,

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

            self.contentHandler = contentHandler
            bestAttemptContent = (request.content.mutableCopy() as?UNMutableNotificationContent)

            bestAttemptContent?.title = request.content.title
            bestAttemptContent?.body = request.content.body

            guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
                return failEarly()
            }

            guard let payload = content.userInfo["CustomData"] as? [String: Any] else {
                return failEarly()
            }

            guard let attachmentURL = payload["Attachement-url"] as? String else {
                return failEarly()
            }


            let identifierName = getIdentifierName(fileURL: attachmentURL)
            let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString

            guard let imageData = NSData(contentsOf:NSURL(string: attachmentURL)! as URL) else { return failEarly() }

            guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: identifierName, data: imageData, options: nil, tmpSubFolderName: tmpSubFolderName) else { return failEarly() }

            content.attachments = [attachment]
            contentHandler(content.copy() as! UNNotificationContent)
        }

    }


    func getIdentifierName(fileURL : String) -> String {
        var identifierName : String = "image.jpg"

        if !fileURL.isEmpty() {
            identifierName = "file.\((fileURL as NSString).lastPathComponent)"
        }

        return identifierName
    }

    func failEarly() {

        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

    extension UNNotificationAttachment {
        static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?, tmpSubFolderName : String) -> UNNotificationAttachment? {

            let fileManager = FileManager.default
            let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
            let fileURLPath      = NSURL(fileURLWithPath: NSTemporaryDirectory())
            let tmpSubFolderURL  = fileURLPath.appendingPathComponent(tmpSubFolderName, isDirectory: true)

            do {
                try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
                let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
                try data.write(to: fileURL!, options: [])
                let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL!, options: options)
                return imageAttachment
            } catch let error {
                print("error \(error)")
            }

            return nil
        }
    }