问题描述
iOS 10 引入了推送通知框架更新,
iOS 10 introduced push notification framework updates,
UserNotificationsUI.framework
正如在 Apple 文档中所写,当本地和远程通知出现在用户设备上时,它让我们可以自定义它们的外观.
As written on apple docs, it lets us customize the appearance of local and remote notifications when they appear on the user’s device.
因此,如果有人知道如何在锁定屏幕上的推送通知中显示图像.和安卓推送通知一样.
So if anyone have idea how to display image in push notification when on lock screen. same like andorid push notification are doing.
谢谢,
推荐答案
如果要自定义本地和远程通知的外观,请执行以下步骤:
If you want to customize the appearance of local and remote notifications, perform the following steps:
创建一个
UNNotificationCategory
并添加到UNUserNotificationCenter
类别:
let newCategory = UNNotificationCategory(identifier: "newCategory",
actions: [ action ],
minimalActions: [ action ],
intentIdentifiers: [],
options: [])
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([newCategory])
创建一个 UNNotificationContentExtension:
Create a UNNotificationContentExtension:
然后使用代码或故事板来自定义您的 UIViewController
.
then use code or storyboard to customize your UIViewController
.
- 将类别添加到
UNNotificationContentExtension
的 plist:
- Add category to
UNNotificationContentExtension
's plist:
4.推送通知
本地通知
创建一个UNMutableNotificationContent
并将categoryIdentifier
设置为newCategory",其中包括UNUserNotificationCenter
的类别和UNNotificationContentExtension
的plist:
Create a UNMutableNotificationContent
and set the categoryIdentifier
to "newCategory" which includes UNUserNotificationCenter
's categories and UNNotificationContentExtension
's plist:
let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"
let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request)
远程通知
设置 "mutable-content : 1"
和 "category : newCategory"
.请注意,类别值设置为newCategory",它与您之前添加到 UNUserNotificationCenter
和 UNNotificationContentExtension
的 plist 中的内容相匹配.
Set "mutable-content : 1"
and "category : newCategory"
. Note that the category value is set to "newCategory" which matches what you previously added to UNUserNotificationCenter
and UNNotificationContentExtension
s plist.
示例:
{
"aps" : {
"alert" : {
"title" : "title",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "newCategory"
},
"otherCustomURL" : "http://www.xxx.jpg"
}
- 注意:您需要一个支持3DTouch的设备或模拟器,否则您无法显示自定义的
UNNotificationContentExtension
视图控制器.(在iOS10 Beta1中,它不起作用.但现在这项工作没有3d touch)
- Note: you need a device or simulator which supports 3DTouch, otherwise you can't show a custom
UNNotificationContentExtension
viewcontroller.(In iOS10 Beta1, it`s not work. But now this work without 3d touch)
而且...如果您只想在锁屏上显示的推送通知上显示图像,则需要添加UNNotificationAttachment
:
And ... if you just want to show an image on a push notification displayed on the lock screen, you need to add UNNotificationAttachment
:
let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"
let fileURL: URL = ... // your disk file url, support image, audio, movie
let attachement = try? UNNotificationAttachment(identifier: "attachment", url: fileURL, options: nil)
content.attachments = [attachement!]
let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request)
更多详细功能,Demo
这篇关于如何在ios推送通知中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!