3

I am trying to create iOS Rich Push notifications with Xcode, Swift3. I already make sure about push notifications (subject, body) with curl command of php but I can't create Rich Push Notifications referred to in this document.

I added Notification Service Extension like this: 「 File 」→「 New 」→「 Target... 」→「 Notification Service Extension 」 and also I added in 「'mutable_content': True」 curl command.

Then run but not call 「class NotificationService: UNNotificationServiceExtension」 so can't view push notifications image.

The following my code

import UserNotifications 
class NotificationService: UNNotificationServiceExtension {

    let imageKey = AnyHashable("gcm.notification.image_url")

    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)

        if let imageUrl = request.content.userInfo[imageKey] as? String {
            let session = URLSession(configuration: URLSessionConfiguration.default)
            let task = session.dataTask(with: URL(string: imageUrl)!, completionHandler: { [weak self] (data, response, error) in
                if let data = data {
                    do {
                        let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("push.png")
                        try data.write(to: writePath)
                        guard let wself = self else {
                            return
                        }
                        if let bestAttemptContent = wself.bestAttemptContent {
                            let attachment = try UNNotificationAttachment(identifier: "nnsnodnb_demo", url: writePath, options: nil)
                            bestAttemptContent.attachments = [attachment]
                            contentHandler(bestAttemptContent)
                        }
                    } catch let error as NSError {
                        print(error.localizedDescription)

                        guard let wself = self else {
                            return
                        }
                        if let bestAttemptContent = wself.bestAttemptContent {
                            contentHandler(bestAttemptContent)
                        }
                    }
                } else if let error = error {
                    print(error.localizedDescription)
                }
            })
            task.resume()
        } else {
            if let bestAttemptContent = bestAttemptContent {
                contentHandler(bestAttemptContent)
            }
        }
    }

    override func serviceExtensionTimeWillExpire() {
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
iwahei0813
  • 61
  • 10

1 Answers1

2

REF : https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications

I have done like this hope it helps for GIF images you can change extension to .png.

  1. Make sure that in APNS payload attachment-url is coming for image.
  2. Check for App transport security key in case of image url start from http://...
  3. Your image should be under ~200px. For me it not works beyond(HIT and TRIAL).

Code:

import UserNotifications
import SDWebImage

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)

        func failEarly() {
            contentHandler(request.content)
        }

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

        guard let attachmentURL = content.userInfo["attachment-url"] as? String else {
            return failEarly()
        }

        SDWebImageDownloader.shared().downloadImage(with: URL(string: attachmentURL)!,
                                                    options: SDWebImageDownloaderOptions.continueInBackground,
                                                    progress: nil) { (image, data, error, flag) in

            guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image.gif",
                                                                    data: data! as NSData,
                                                                    options: nil) else { return failEarly() }
            content.attachments = [attachment]
            contentHandler(content.copy() as! UNNotificationContent)

            if let bestAttemptContent = self.bestAttemptContent {
                bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
                contentHandler(bestAttemptContent)
            }
        }
    }

    override func serviceExtensionTimeWillExpire() {
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

extension UNNotificationAttachment {
    static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
        let fileManager = FileManager.default
        let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
        let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).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(identifier: imageFileIdentifier, url: fileURL!, options: options)
            return imageAttachment
        } catch let error {
            print("error \(error)")
        }

        return nil
    }
}
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
  • really really thank you post for my question. i made a question here [link](https://stackoverflow.com/questions/48903594/cant-get-image-ios-rich-push-notifications-with-xcode-swift3) as a result i couldnt get image... if you have any ideas please answer again thank you very much. @Abhishek Thapliyal – iwahei0813 Feb 22 '18 at 02:18
  • you just resize image size u will get image 100*100px – Abhishek Thapliyal Feb 22 '18 at 04:27
  • like : https://humethnet.files.wordpress.com/2016/02/100px-biohazard_symbol_red-svg.png – Abhishek Thapliyal Feb 22 '18 at 04:31
  • thank you very much for replying @Abhishek Thapliyal. Immediately tried the image resize but still dosent work... In the first place dont run 「class NotificationService: UNNotificationServiceExtension」 any ideas?? sorry again and thank you very mush. – iwahei0813 Feb 22 '18 at 07:08
  • can you check url is getting correctly and if HTTP then app trnas port security issue ? – Abhishek Thapliyal Feb 22 '18 at 13:05
  • thank you very much for replying @Abhishek Thapliyal. i added in the code NotificationService(info.plist) **NSAppTransportSecurity NSAllowsArbitraryLoads ** then workin! thank you sosososososo much! But i wonder why not output print("hogehogehogehoge") is it specification or my misstake?? – iwahei0813 Feb 23 '18 at 02:58
  • HAHAHA :p extension logs in console is the bug in xcode, enjoy these bugs, i also failed to achieve logs :p :D can you upvote my answer as well – Abhishek Thapliyal Feb 23 '18 at 03:03