【问题标题】:how to display image in ios push notification?如何在 ios 推送通知中显示图像?
【发布时间】:2016-10-16 18:56:20
【问题描述】:

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

UserNotificationsUI.framework

正如苹果文档中所写,当本地和远程通知出现在用户设备上时,它可以让我们自定义其外观。

因此,如果有人知道如何在锁定屏幕时在推送通知中显示图像。就像 andorid 推送通知一样。

谢谢,

【问题讨论】:

标签: ios push-notification apple-push-notifications ios10 unnotificationserviceextension


【解决方案1】:

如果要自定义本地和远程通知的外观,请执行以下步骤:

  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”,它与您之前添加到UNUserNotificationCenterUNNotificationContentExtensions plist 的内容相匹配。

例子:

 {
    "aps" : {
        "alert" : {
        "title" : "title",
        "body" : "Your message Here"
        },
        "mutable-content" : "1",
        "category" : "newCategory"
    },
    "otherCustomURL" : "http://www.xxx.jpg"
 }
  1. 注意:需要支持3DTouch的设备或模拟器,否则无法显示自定义UNNotificationContentExtensionviewcontroller。(在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

【讨论】:

  • 如果我错了,请纠正我。在第 5 点中,您是说这一切都无法在没有 3d touch 的设备中完成?
  • @maquannene 我很确定你必须设置 "mutable-content": 1 ;即,1 整数,而不是 "1" 字符串,还是我不正确?
  • 什么是 UNNotificationAttachment 中的“附件” - 标识符...?我在这一行出现错误-(在展开可选值时意外发现 nil)
  • @maquannene : 你能添加一个简单的演示项目来在远程通知中显示图像吗?
  • 这个答案是误导性的。为什么?因为 OP 正在询问 remote 通知。但答案主要是显示从创建通知时起如何附加和烘焙图像 - 本地。获得远程通知显示其图像的唯一方法是拥有NotificationServiceExtensionNotificationContentExtension 不起作用,因为它只是呈现传递给它的现有内容。附件必须在本地传递或下载到NotificationServiceExtension。有关更多信息,请参阅developer.apple.com/videos/play/wwdc2016/708
【解决方案2】:

实际上,如果您正在设置本地通知并且您只是对在通知本身中显示图像感兴趣,那么您根本不必为 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 等设置内容扩展。

编辑:更新添加这只是本地通知的有效解决方案。

【讨论】:

  • 这是否适用于远程文件路径,即webservice/images/myimage.png
  • 不好意思,没试过,不知道有没有。为什么不试一试,看看有没有?
【解决方案3】:

您必须在创建推送通知以及处理时做一些工作。

  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)
    }
    

Here 是关于这个主题的 WWDC 视频演讲。

【讨论】:

  • mutable-content 是需要添加到aps字典中的key。
  • 你还需要下载图片
【解决方案4】:

使用符合 UNNotificationContentExtension 的 UIViewController 实现通知的自定义视图。

https://developer.apple.com/reference/usernotificationsui/unnotificationcontentextension

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多