添加图像和视频 iOS image-push

NOTE
本文档仅适用于iOS设备。

在本文档中,了解如何显示Adobe Campaign Standard iOS推送通知中的图像。

步骤1:设置推送通知 set-up-push

Experience PlatformSDK支持推送通知。

接收推送通知的移动应用程序必须由管理员在Adobe Campaign界面中配置。

通过配置Adobe Campaign和AdobeMobile Services,您可以将移动应用程序的数据用于营销活动。 有关详细信息,请参见此 页面

要使用Experience CloudSDK应用程序发送推送通知,必须在数据收集UI中设置移动应用程序,并在Adobe Campaign中进行配置。 有关详细信息,请参见此 页面

步骤2:在Adobe Campaign中自定义推送通知 customize-push

要优化推送通知消息,可在设计推送通知消息时通过 Adobe Campaign 访问一组高级选项。

  1. 创建推送通知。 有关详细信息,请参见此 页面

  2. 从推送通知内容页面,访问 Advanced options 部分。

  3. 在中输入文件的URL Rich media content URL 字段。
    对于iOS 10或更高版本,您可以插入图像、gif、音频和视频文件。

  4. 预览并保存推送通知。

步骤3:调整移动设备应用程序代码 mobile-app-code

在Adobe Campaign中自定义推送通知后,您必须配置移动应用程序以在设备上显示图像。

NOTE
如果您的应用程序在Objective-C中,请参阅以下内容 文档.

如果您的应用程序位于 Swift,请按照以下步骤操作:

  1. 打开您的 Xcode 项目。

  2. 在您的 Xcode 项目,选择 File > New > Target.

  3. 选择 Notification Service Extension

  4. 检查 NotificationService.swift 创建文件类。

  5. 编辑此类并使用以下内容替换默认内容。
    这允许应用程序使用图像URL处理传入参数,解析该参数,将其复制到本地,然后从推送通知中显示该参数。

    code language-none
    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使用键media-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;

}]

步骤4:测试发送推送 test-send-push

您现在可以测试构建应用程序以及在上述步骤2中创建的投放。 有关准备和发送推送通知的更多信息,请参阅此 页面.

recommendation-more-help
3ef63344-7f3d-48f9-85ed-02bf569c4fff