[英]how to display image in ios push notification?
iOS 10 引入了推送通知框架更新,
UserNotificationsUI.framework
正如 Apple 文檔中所寫,當本地和遠程通知出現在用戶的設備上時,它讓我們可以自定義它們的外觀。
因此,如果有人知道如何在鎖定屏幕上的推送通知中顯示圖像。 就像andorid推送通知正在做的一樣。
謝謝,
如果要自定義本地和遠程通知的外觀,請執行以下步驟:
創建一個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
和UNNotificationContentExtension
s plist 的內容相匹配。
示例:
{
"aps" : {
"alert" : {
"title" : "title",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "newCategory"
},
"otherCustomURL" : "http://www.xxx.jpg"
}
UNNotificationContentExtension
viewcontroller。(在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
實際上,如果您正在設置本地通知並且您只是對在通知中顯示圖像感興趣,那么您根本不必理會 NotificationsUI.framework 或 UNNotificationContentExtensions。 這僅在用戶 3D 觸摸或展開通知時想要自定義 UI 時才有用。
要添加設備上已經存在的圖像(應用程序附帶的圖像,或者在創建通知之前的某個時間點由應用程序生成/存儲的圖像),那么您只需要添加一個帶有路徑結尾的文件 URL 的 UNNotificationAttachment在 .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 等設置內容擴展的所有環節。
編輯:更新以補充說這只是本地通知的有效解決方案。
您必須在創建推送通知以及處理時做一些工作。
創建有效負載時,您需要添加額外的屬性附件,如下所示:
{ 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) }
這是關於這個主題的 WWDC 視頻演講。
使用符合 UNNotificationContentExtension 的 UIViewController 實現通知的自定義視圖。
請參閱https://developer.apple.com/reference/usernotificationsui/unnotificationcontentextension
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.