如果要自定义本地和远程通知的外观,请执行以下步骤:
-
创建一个UNNotificationCategory 并添加到UNUserNotificationCenter 类别:
let newCategory = UNNotificationCategory(identifier: "newCategory",
actions: [ action ],
minimalActions: [ action ],
intentIdentifiers: [],
options: [])
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([newCategory])
创建一个 UNNotificationContentExtension:
然后使用代码或故事板自定义您的UIViewController。
- 将类别添加到
UNNotificationContentExtension 的plist:
4.推送通知
本地通知
创建一个UNMutableNotificationContent 并将categoryIdentifier 设置为“newCategory”,其中包括UNUserNotificationCenter 的类别和UNNotificationContentExtension 的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 和UNNotificationContentExtensions plist 的内容相匹配。
例子:
{
"aps" : {
"alert" : {
"title" : "title",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "newCategory"
},
"otherCustomURL" : "http://www.xxx.jpg"
}
- 注意:需要支持3DTouch的设备或模拟器,否则无法显示自定义
UNNotificationContentExtensionviewcontroller。(在iOS10 Beta1中无法使用。但是现在这个作品没有3d touch)
而且...如果你只是想在锁屏上显示的推送通知上显示图片,你需要添加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