Images in iOS push notification

前端 未结 2 1694
广开言路
广开言路 2020-12-12 07:13

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

相关标签:
2条回答
  • 2020-12-12 08:02

    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
            }
        }
    
    0 讨论(0)
  • 2020-12-12 08:07

    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.

    0 讨论(0)
提交回复
热议问题