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 推送通知中的图像


- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01