如何在ios推送通知中显示图像?

Par*_*dya 45 push-notification apple-push-notifications ios ios10

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

UserNotificationsUI.framework

正如在Apple docs上所写,它允许我们在用户设备上显示本地和远程通知时自定义它们的外观.

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

谢谢,

maq*_*ene 80

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

  1. 创建UNNotificationCategory并添加到UNUserNotificationCenter类别:

    let newCategory = UNNotificationCategory(identifier: "newCategory",
                                             actions: [ action ],
                                             minimalActions: [ action ],
                                             intentIdentifiers: [],
                                             options: [])
    
    let center = UNUserNotificationCenter.current()
    
    center.setNotificationCategories([newCategory])
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个UNNotificationContentExtension:

在此输入图像描述

然后使用代码或故事板来自定义您的UIViewController.

  1. 将类别添加到UNNotificationContentExtension's plist:

在此输入图像描述

4.推送通知

本地通知

创建一个UNMutableNotificationContent并设置categoryIdentifier为"newCategory",其中包含UNUserNotificationCenter的类别和UNNotificationContentExtensionplist:

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)
Run Code Online (Sandbox Code Playgroud)

远程通知

设置"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"
 }
Run Code Online (Sandbox Code Playgroud)
  1. 注意:您需要一个支持3DTouch的设备或模拟器,否则您无法显示自定义UNNotificationContentExtension视图控制器.(在iOS10 Beta1中,它不起作用.但现在这项工作没有3D触摸)

并且......如果您只想在锁定屏幕上显示的推送通知上显示图像,则需要添加 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有关更多详细信息,请参阅演示

  • 这个答案具有误导性。为什么?因为OP正在询问**远程**通知。但答案主要是显示图像在创建通知时如何附加和烘焙 - **本地**。获得远程通知显示其图像的唯一方法是使用“NotificationServiceExtension”。`NotificationContentExtension` 不起作用,因为它只是渲染传递给它的现有内容。附件必须在本地传递或在“NotificationServiceExtension”中下载。有关更多信息,请参阅 https://developer.apple.com/videos/play/wwdc2016/708/ (2认同)

Gre*_*g G 10

实际上,如果您正在设置本地通知,并且您只想在通知本身中显示图像,则根本不需要使用NotificationsUI.framework或UNNotificationContentExtensions.这仅在用户3D触摸或扩展通知时需要自定义UI时才有用.

要添加设备上已有的图像(随应用程序一起提供,或者在创建通知之前的某个时刻由应用程序生成/存储),您只需要添加一个UNNotificationAttachment,其中包含路径结束的文件URL在.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];
}
Run Code Online (Sandbox Code Playgroud)

然后,当通知出现时,图像将显示在通知横幅的右侧,就像消息通知一样.而且您不必跳过使用categoryIdentifier等设置内容扩展的所有环节.

编辑:更新以补充说这只是本地通知的有效解决方案.


Adn*_*tab 5

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

  1. 创建有效负载时,您需要添加额外的属性附件,如下所示:

    {        
        aps : {
            alert: { },
            mutable-content:1
        }
        my-attachment = "url to resource"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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)
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

  • 您还需要下载图像 (2认同)

Pet*_*ski -2

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

请参阅https://developer.apple.com/reference/usernotificationsui/unnotificationcontentextension