Images in iOS push notification

前端 未结 2 1699
广开言路
广开言路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
            }
        }
    

提交回复
热议问题