【问题标题】:I am using Notification Service Extension but I am unable to get the image in the notification, How could I get the image in the Notification Center?我正在使用通知服务扩展,但无法在通知中获取图像,如何在通知中心获取图像?
【发布时间】:2018-01-25 10:19:39
【问题描述】:

我正在使用带有开发配置文件的推送器应用程序来获取丰富的推送通知,并在 iOS 上的通知中心右侧附加图像,但我无法在通知上获取图像,而我可以看到标题、副标题和身体在推。我需要在通知服务扩展中添加任何其他代码吗?

{"aps" : {
        "alert" : {
            "title" : "Introduction To Notification",
            "subtitle" : "Session 707",
            "body" : "New Notification Look Amazing"
        },
       "sound" : "default",
        "category" : "message",
        "badge" : 1,
        "mutable-content": 1
    },
    "attachment-url": "https://i.imgur.com/t4WGJQx.jpg"
}

【问题讨论】:

    标签: ios objective-c push-notification notifications


    【解决方案1】:

    是的,您需要自己下载图像并在下载完成后调用完成处理程序。

    您的“通知扩展”必须有一个 viewController,您可能在其中添加了名为“mainInterface.storyBoard”的故事板中的 UI。所以在你的viewcontroller类中假设NotificationViewController,它需要实现协议:UNNotificationContentExtension,然后委托方法你可以实现为:

    class NotificationViewController: UIViewController, UNNotificationContentExtension {
    
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any required interface initialization here.
    }
    
    func didReceive(_ notification: UNNotification) {
        if let urlString = notification.request.content.userInfo["attachment-url"] as! String? {
            if let imageURL = URL(string: urlString) {
                if let data = NSData(contentsOf: imageURL) {
                    self.imageView.image = UIImage(data: data as Data)
                }    
            }
        }  
      }
    }
    

    这里的图像视图是添加到视图控制器中的图像视图的出口。

    附:您需要将框架导入为:

    import UserNotifications
    import UserNotificationsUI
    

    您可以参考自定义通知教程here

    【讨论】:

    • 谢谢!!!在所有指南和文档中,我都找不到这样的例子。它现在正在工作。
    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2018-05-04
    • 2012-09-30
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多