本檔案僅適用於iOS裝置。
在本檔案中,瞭解如何從Adobe Campaign Standard iOS推播通知顯示影像。
推播通知受Experience Platform SDK支援。
接收推播通知的行動應用程式必須由管理員在Adobe Campaign介面中設定。
透過設定Adobe Campaign和Adobe Mobile Services,您將能夠將行動應用程式的資料用於促銷活動。 如需關於此項目的詳細資訊,請參閱此頁面。
若要使用Experience Cloud SDK應用程式傳送推播通知,行動應用程式必須在Adobe Experience Platform Launch中設定,並在Adobe Campaign中設定。 如需關於此項目的詳細資訊,請參閱此頁面。
為了微調您的推播通知,Adobe Campaign 可讓您在設計推播通知時存取一組進階選項。
自訂推播通知. 如需關於此項目的詳細資訊,請參閱此頁面。
從您的推播通知內容頁面,存取Advanced options區段。
在Rich media content URL欄位中輸入檔案的URL。
若是 iOS 10 或更新版本,您可以插入影像、gif、音訊和視訊檔案。
預覽並儲存您的推播通知。
在Adobe Campaign中自訂推播通知後,您必須設定行動應用程式,才能在裝置上顯示影像。
如果您的應用程式位於Objective-C中,請參閱以下檔案。
如果您的應用程式位於Swift中,請遵循下列步驟:
開啟您的Xcode專案。
在Xcode專案中,選取File > New > Target。
選取 Notification Service Extension。
檢查是否已建立NotificationService.swift檔案類。
編輯此類別,並以下列項目取代預設內容。
這可讓應用程式使用影像URL處理傳入的參數、加以剖析、在本機複製,然後從推播通知中顯示。
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
var urlString:String? = nil
if let urlImageString = request.content.userInfo["media-attachment-url"] as? String {
urlString = urlImageString
}
if urlString != nil, let fileUrl = URL(string: urlString!) {
print("fileUrl: \(fileUrl)")
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
// Move temporary file to remove .tmp extension
if (error == nil) {
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
try! FileManager.default.moveItem(at: location, to: tmpUrl)
// Add the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: fileUrl.lastPathComponent, url: tmpUrl) {
bestAttemptContent.attachments = [attachment]
}
}
if(error != nil) {
print("Failed to download attachment: \(error.debugDescription)")
}
}
// Serve the notification content
contentHandler(bestAttemptContent)
}.resume()
}
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
傳送通知時,行動裝置應會收到下列裝載。
影像URL會與關鍵媒體附件URL對應。 這是您必須從應用程式程式碼角度處理的索引鍵/值組,才能下載和顯示影像。
userInfo: [AnyHashable("media-attachment-url"): https://pbs.twimg.com/profile_images/876737835314950144/zPTs9b7o.jpg, AnyHashable("_dId"): 1de3ef93, AnyHashable("_mId"): h280a5, AnyHashable("aps"): {
alert = {
body = "Message Body here";
title = "This a push from Campaign";
};
badge = 1;
"mutable-content" = 1;
}]
您現在可以測試建立應用程式以及您在上述步驟2中建立的傳送。 如需準備和傳送推播通知的詳細資訊,請參閱此頁面。