本文介绍了如何在ios推送通知中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

iOS 10 introduced push notification framework updates,

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

As written on apple docs, it lets us customize the appearance of local and remote notifications when they appear on the user’s device.

所以如果有人知道如何在锁定屏幕上显示推送通知中的图像。和andorid推送通知一样。

So if anyone have idea how to display image in push notification when on lock screen. same like andorid push notification are doing.

谢谢,

推荐答案

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

If you want to customize the appearance of local and remote notifications, perform the following steps:


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

let newCategory = UNNotificationCategory(identifier: "newCategory",
                                         actions: [ action ],
                                         minimalActions: [ action ],
                                         intentIdentifiers: [],
                                         options: [])

let center = UNUserNotificationCenter.current()

center.setNotificationCategories([newCategory])


  • 创建一个UNNotificationContentExtension:

  • Create a UNNotificationContentExtension:

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

    then use code or storyboard to customize your UIViewController.


    1. 将类别添加到 UNNotificationContentExtension 的plist:

    1. Add category to UNNotificationContentExtension's plist:

    4.Push Notification

    4.Push Notification

    本地通知

    创建 UNMutableNotificationContent 并设置 categoryIdentifier 到newCategory,其中包括 UNUserNotificationCenter 的类别和 UNNotificationContentExtension 的plist:

    Create a UNMutableNotificationContent and set the categoryIdentifier to "newCategory" which includes UNUserNotificationCenter's categories and UNNotificationContentExtension's 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:1category:newCategory。请注意,类别值设置为newCategory,它与您之前添加到 UNUserNotificationCenter UNNotificationContentExtension s plist的内容相匹配。

    Set "mutable-content : 1" and "category : newCategory". Note that the category value is set to "newCategory" which matches what you previously added to UNUserNotificationCenter and UNNotificationContentExtensions plist.

    示例:

     {
        "aps" : {
            "alert" : {
            "title" : "title",
            "body" : "Your message Here"
            },
            "mutable-content" : "1",
            "category" : "newCategory"
        },
        "otherCustomURL" : "http://www.xxx.jpg"
     }
    




    1. 注意:您需要支持3DTouch的设备或模拟器,否则你不能显示自定义 UNNotificationContentExtension viewcontroller。(在iOS10 Beta1中,它不起作用。但现在这项工作没有3D触摸)

    1. Note: you need a device or simulator which supports 3DTouch, otherwise you can't show a custom UNNotificationContentExtension viewcontroller.(In iOS10 Beta1, it`s not work. But now this work without 3d touch)

    并且......如果你只是想在显示的推送通知上显示图像锁定屏幕,你需要添加 UNNotificationAttachment

    And ... if you just want to show an image on a push notification displayed on the lock screen, you need to add 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)
    

    有关详细信息,

    这篇关于如何在ios推送通知中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-05 05:11