1

我们如何像以前在富通知中显示的那样显示带有本地通知的图像或任何附件?

我收到静默通知,然后在本地通知中更改它。

4

1 回答 1

3

是的,您也可以在本地通知中显示它,您必须在收到静默推送后触发本地通知,我希望您的静默通知有效负载中所有需要的数据都在那里。

这是代码片段

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

完成此操作后,它会正常工作,如果您仍然遇到任何问题,请告诉我。

于 2018-10-12T05:56:32.450 回答