如何在ios推送通知中显示图像?
iOS 10引入了推送通知框架更新,
UserNotificationsUI.framework
正如在苹果文档上所写的,它让我们可以在用户设备上显示本地和远程通知时自定义外观。
所以如果有人想知道如何在locking屏幕上显示推送通知中的图像。 像andorid推送通知一样正在做。
谢谢,
如果要自定义本地和远程通知的外观,请执行以下步骤:
-
创build一个
UNNotificationCategory
并添加到UNUserNotificationCenter
类别中:let newCategory = UNNotificationCategory(identifier: "newCategory", actions: [ action ], minimalActions: [ action ], intentIdentifiers: [], options: []) let center = UNUserNotificationCenter.current() center.setNotificationCategories([newCategory])
-
创build一个UNNotificationContentExtension:
然后使用代码或故事板来定制你的UIViewController
。
- 将类别添加到
UNNotificationContentExtension
的plist:
4.推送通知
本地通知
创build一个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
和UNNotificationContentExtension
的plist的内容相匹配。
例:
{ "aps" : { "alert" : { "title" : "title", "body" : "Your message Here" }, "mutable-content" : "1", "category" : "newCategory" }, "otherCustomURL" : "http://www.xxx.jpg" }
- 注意:你需要一个支持3DTouch的设备或者模拟器,否则你不能显示一个自定义的
UNNotificationContentExtension
(在iOS10 Beta1中,它不工作。 但是现在这个没有3D触摸的工作)
而且,如果您只想在locking屏幕上显示的推送通知上显示图像,则需要添加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)
有关更多的细节function, 演示
实际上,如果您正在设置本地通知,而您只是希望在通知本身中显示图像,则根本不必打扰NotificationsUI.framework或UNNotificationContentExtensions。 这只有在用户3D触摸或扩展通知时需要自定义用户界面时才有用。
要添加已经在设备上的图像(与应用程序一起提供,或者在创build通知之前的某个时间点由应用程序生成/存储),则只需添加一个UNNotificationAttachment,其中包含path结束的文件URL在.png(或.jpg也可能工作?)。 像这样的东西:
UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"Title"; content.body = @"Body"; content.sound = [UNNotificationSound defaultSound]; NSURL *imageURL = [NSURL URLWithString:@"file:/some/path/in/app/image.png"]; NSError *error; UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageURL options:nil error:&error]; if (error) { NSLog(@"error while storing image attachment in notification: %@", error); } if (icon) { content.attachments = @[icon]; }
然后,当通知出现时,图像将显示在通知横幅的右侧,就像它对邮件通知所做的一样。 而且您不必跳过所有使用categoryIdentifier等设置内容扩展的方法
编辑:更新以补充说,这只是一个有效的本地通知解决scheme。
你必须做一些创build推送通知的工作,也是在你处理的时候。
-
当你创build有效载荷时,你需要添加额外的属性附件,如下所示:
{ aps : { alert: { }, mutable-content:1 } my-attachment = "url to resource" }
-
当你收到通知系统会调用
didReceive
方法的服务扩展,重写通知扩展didReceive
方法就像这样public func didReceive(_ request:UNNotificationRequest, withContentHandler contentHandler:(UNNotificatinContent) -> Void) { let fileUrl = // let attachment = UNNotificationAttachment(identifier : "image", url: fileUrl, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachment = [attachment] contentHandler(content) }
这是关于这个话题的WWDCvideo谈话。
使用符合UNNotificationContentExtension的UIViewController实现通知的自定义视图。
请参阅https://developer.apple.com/reference/usernotificationsui/unnotificationcontentextension