此文档仅适用于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将映射为键媒体 — attachment-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中创建的投放。 有关准备和发送推送通知的详细信息,请参阅此页面。