Images in iOS push notification(iOS 推送通知中的图像)
问题描述
我正在尝试在推送通知中发送 images
我已经在应用程序委托中进行了通知注册,并且 apns 设备令牌正在正确生成.另外我在服务分机中编码如下:
I am trying to send images
in push notifications
I have made the notifications registrations in app delegate and apns device token is generating properly.
ALso I have coded in service ext as follows:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let notificationData = request.content.userInfo["data"] as? [String: String] {
// Grab the attachment
if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
// Move temporary file to remove .tmp extension
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
try! FileManager.default.moveItem(at: location, to: tmpUrl)
// Add the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
self.bestAttemptContent?.attachments = [attachment]
}
}
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
}
}
.而json中的payload如下
. And the payload in json is as follows
{
"aps":
{"sound":"default","alert":
{"title":"iOS","body":"Hello Dude...."},
"mutable-content": 1},
"CustomData":
{"mType":"alert","m":"Hello Dude...."},
"Attachement-url":"https://pusher.com/static_logos/320x320.png"
}
我收到 title 和 message,但 image 没有收到.请指导如何在推送通知中获取图像
I am receiving the title and message but image is not coming. Please guide how to get image in push notifications
推荐答案
这一行:
if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
正在寻找 attachment-url
值作为 userInfo 字典中 data
对象的子项.它正在寻找这个:
Is looking for an attachment-url
value as a child of a data
object in the userInfo dictionary.
It is looking for this:
{
"aps" : {
...
},
"data" : {
"attachment-url" : "some url"
}
}
但您问题中的有效负载是这样的:
But the payload in your question is this:
{
"aps":{
"sound":"default",
"alert": {
"title":"iOS",
"body":"Hello Dude...."
},
"mutable-content": 1
},
"CustomData": {
"mType":"alert",
"m":"Hello Dude...."
},
"Attachement-url":"https://pusher.com/static_logos/320x320.png"
}
data"部分不存在,attachment-url
键也不存在.
The "data" section does not exist, and the attachment-url
key does not exist.
更改您的 Swift 代码以匹配有效负载中的内容,您应该能够获取图像 URL 并下载它.
Change your Swift code to match what is in the payload and you should be able to get the image URL and download it.
如果您收到通知没有有附件 URL 键或附件 URL 不是格式正确的 URL,您将遇到大问题.在这些情况下,您的 if let
将不会被输入,并且 contentHandler
也不会被调用!这不仅会导致服务扩展锁定,而且会阻止任何没有附件 URL 的通知被传递!添加一个调用 contentHandler
的 else
来解决这个问题.
You will have a big problem if you receive a notification that does not have the attachment URL key or the attachment URL is not a properly formed URL. In those cases your if let
will not be entered and the contentHandler
will not be called! This will not just cause the service extension to lock up, but it will prevent any notification that does not have the attachment URL from being delivered! Add an else
that calls the contentHandler
to fix this.
一旦你下载了它,虽然还有另一个问题.iOS 需要知道您在附件中放入了什么样的数据.附件选项字典允许您包含有关附件的类型信息.获取下载文件的 MIME 类型并从中创建统一类型标识符.然后可以在选项字典中使用统一类型标识符字符串.
Once you have it downloaded though there is another problem. iOS will need to know what kind of data you are putting in the attachment. The attachment options dictionary allows you to include type information about the attachment. Get the MIME Type of the downloaded file and create a Uniform Type Identifier from that. The Uniform Type Identifier string can then be used in the options dictionary.
我在 iOS 通知书 中详细介绍了所有这些内容.现在可用的示例章节处理向通知添加图像.
I cover all of this in depth in the iOS Notifications book. The sample chapter available now deals with adding images to notifications.
这篇关于iOS 推送通知中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 推送通知中的图像
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01