簡體   English   中英

如何在ios推送通知中顯示圖像?

[英]how to display image in ios push notification?

iOS 10 引入了推送通知框架更新,

UserNotificationsUI.framework

正如 Apple 文檔中所寫,當本地和遠程通知出現在用戶的設備上時,它讓我們可以自定義它們的外觀。

因此,如果有人知道如何在鎖定屏幕上的推送通知中顯示圖像。 就像andorid推送通知正在做的一樣。

謝謝,

如果要自定義本地和遠程通知的外觀,請執行以下步驟:

  1. 創建一個UNNotificationCategory並添加到UNUserNotificationCenter類別中:

     let newCategory = UNNotificationCategory(identifier: "newCategory", actions: [ action ], minimalActions: [ action ], intentIdentifiers: [], options: []) let center = UNUserNotificationCenter.current() center.setNotificationCategories([newCategory])
  2. 創建一個 UNNotificationContentExtension:

在此處輸入圖片說明

然后使用代碼或故事板來自定義您的UIViewController

  1. 將類別添加到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”,它與您之前添加到UNUserNotificationCenterUNNotificationContentExtension s plist 的內容相匹配。

示例:

 {
    "aps" : {
        "alert" : {
        "title" : "title",
        "body" : "Your message Here"
        },
        "mutable-content" : "1",
        "category" : "newCategory"
    },
    "otherCustomURL" : "http://www.xxx.jpg"
 }
  1. 注意:您需要一個支持3DTouch的設備或模擬器,否則您無法顯示自定義的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 等設置內容擴展的所有環節。

編輯:更新以補充說這只是本地通知的有效解決方案。

您必須在創建推送通知以及處理時做一些工作。

  1. 創建有效負載時,您需要添加額外的屬性附件,如下所示:

     { aps : { alert: { }, mutable-content:1 } my-attachment = "url to resource" }
  2. 當您收到通知,系統會調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM