Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我正在使用通知服务扩展,但无法在通知中获取图像,如何在通知中心获取图像?_Ios_Objective C_Push Notification_Notifications - Fatal编程技术网

Ios 我正在使用通知服务扩展,但无法在通知中获取图像,如何在通知中心获取图像?

Ios 我正在使用通知服务扩展,但无法在通知中获取图像,如何在通知中心获取图像?,ios,objective-c,push-notification,notifications,Ios,Objective C,Push Notification,Notifications,我正在使用具有开发配置文件的pusher应用程序获取丰富的推送通知,图像附在iOS通知中心的右侧,但我无法获取通知上的图像,而我可以在推送中看到标题、字幕和正文。在通知服务扩展中是否需要执行其他代码 {"aps" : { "alert" : { "title" : "Introduction To Notification", "subtitle" : "Session 707", "body" : "New

我正在使用具有开发配置文件的pusher应用程序获取丰富的推送通知,图像附在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"
}

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

“通知扩展名”必须有一个viewController,您可以在其中将UI添加到名为“mainInterface.storyboard”的情节提要中。因此,在viewcontroller类中,比如说NotificationViewController,它需要实现protocol: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

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

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

“通知扩展名”必须有一个viewController,您可以在其中将UI添加到名为“mainInterface.storyboard”的情节提要中。因此,在viewcontroller类中,比如说NotificationViewController,它需要实现protocol: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

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

谢谢!!!在所有的指南和文档中,我都找不到这样的例子。现在开始工作了。谢谢!!!在所有的指南和文档中,我都找不到这样的例子。它正在工作。