1

How can we show the image or any attachment with local notifications as we used to show in rich notifications ?

I am receiving the silent notification and then changing it in local notification.

shivi_shub
  • 1,018
  • 1
  • 7
  • 15

1 Answers1

2

Yes, you can show it in the local notification also, you have to trigger local notification after you received silent push , I hope in your slient notification payload all require data is there.

Here is the code snippet

let content = UNMutableNotificationContent()

//Configure notification 

content.title = "Notification Title"
content.body = "Notification Body"
content.sound = UNNotificationSound.default()  
content.categoryIdentifier = "ImageNotification"


//Attach your image local path here (Document dir path)

let attachment = try! UNNotificationAttachment(identifier: "\(NSDate().timeIntervalSince1970 * 1000)", url: localURL, options: [:])
content.attachments = [attachment]


content.userInfo = ["attachmentType": "Media"]

// Create a trigger for fire a local notification

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.2, repeats: false)        
let request = UNNotificationRequest(identifier: "\(NSDate().timeIntervalSince1970 * 1000)", content: content, trigger: trigger)

// Configure according to version

if #available(iOS 11.0, *) {
let contactCategory = UNNotificationCategory(identifier: content.categoryIdentifier,
                                                         actions: [],
                                                         intentIdentifiers: [],
                                                         hiddenPreviewsBodyPlaceholder: "",
                                                         options: .customDismissAction)
let notificationCenter = UNUserNotificationCenter.current()
            notificationCenter.setNotificationCategories([contactCategory])
} else {
        // Fallback on earlier versions

let contactCategory = UNNotificationCategory(identifier: content.categoryIdentifier, actions: [], intentIdentifiers: [], options: [])
            let notificationCenter = UNUserNotificationCenter.current()
            notificationCenter.setNotificationCategories([contactCategory])
        }


UNUserNotificationCenter.current().add(request) {[weak self] (error) in
    guard error == nil else { 
                return
    }
}

After this implement it would be work fine, If you still facing any issue so please let me know.