Nit*_*ish 0 apple-push-notifications ios swift unnotificationserviceextension
我逐步移动以获得丰富的推送通知。他们来了 :
NotificationService didRecieve:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
func failEarly() {
contentHandler(request.content)
}
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let data = request.content.userInfo as? [String: AnyObject] {
// Grab the attachment
// let notificationData = data["data"] as? [String: String]
if let urlString = data["attachment-url"], let fileUrl = URL(string: urlString as! String) {
// 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: "video", url: tmpUrl, options:nil) {
self.bestAttemptContent?.attachments = [attachment]
}else if let attachment = try? UNNotificationAttachment(identifier: "image", url: tmpUrl, options:nil) {
self.bestAttemptContent?.attachments = [attachment]
}else if let attachment = try? UNNotificationAttachment(identifier: "audio", url: tmpUrl, options:nil) {
self.bestAttemptContent?.attachments = [attachment]
}else if let attachment = try? UNNotificationAttachment(identifier: "image.gif", url: tmpUrl, options: nil) {
self.bestAttemptContent?.attachments = [attachment]
}
}
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
}
Run Code Online (Sandbox Code Playgroud)
丰富的通知正确发出:
但是,这里是我面临的问题:
有效负载:
aps = {
alert = "This is what your message will look like! Type in your message in the text area and get a preview right here";
badge = 1;
"mutable-content" = 1;
sound = default;
};
"attachment-url" = "https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4";
deeplinkurl = "";
"message_id" = 1609;
}
Run Code Online (Sandbox Code Playgroud)
我确实尝试过以下帖子,但没有帮助:
iOS10 UNNotificationServiceExtension不称为
NotificationServiceExtension不称为
UNNotificationServiceExtension在iPhone 5(iOS 10)上不起作用
好消息!实际上,您的服务扩展名已被调用-通知中的图像即证明了这一点。这里可能发生的情况是,您无法使用应用程序惯用的工作流来调试扩展。
调试通知扩展与调试应用程序不同。扩展程序是应用程序外部的iOS进程的插件。仅仅设置一个断点并不是调试它们的可靠方法。代替:
调试Notification Service扩展
发出通知后,服务扩展应启动到调试器。服务扩展仅与远程(推送)通知相关,因此您将需要一台设备来对其进行故障排除。
调试通知内容扩展 至少有两种方法。上面显示的服务扩展步骤也适用于内容扩展。第二种方法比较熟悉,但可靠性较低。



值得注意的是,使用日志记录框架添加日志记录对于调试和故障排除也非常有用。
iOS限制了可以在通知中显示的内容的大小。UNNotificationAttachment的文档中对此进行了描述。对于视频,通常为50Mb。确保您的视频尽可能小(以字节为单位),并且当然要提供适合于将要播放的设备的视频大小。不要尝试在400点宽的通知中播放1080p视频!
实际上,使用HLS而不是下载视频并将其显示在内容扩展中几乎总是更好的选择。
代码中可能会出现问题的另一件事是您分配给附件的标识符。标识符应该是唯一的。通常,这是一个反向域符号字符串,例如您的捆绑软件ID,后跟一个UUID字符串。您还可以使用内容的原始URL,后跟UUID字符串。如果您提供一个空字符串,iOS将为您创建一个唯一标识符。对于具有非唯一标识符(用于通知,附件等)的用户通知框架,往往会导致难以跟踪框架内部的问题。例如,这可能导致连接的watchOS设备崩溃。
如果您想为视频实现“自动播放”-从您的问题还不清楚这是您要描述的内容-您将需要在内容扩展中实现自己的播放器功能。
如果您要这样做,HLS是在通知中显示视频的首选方式。它通常使用较少的RAM,提供更好的用户体验,并趋于更稳定。
| 归档时间: |
|
| 查看次数: |
1445 次 |
| 最近记录: |