为了获得丰富的推送通知,我一步步前进。他们在这里:
- 使用 plist 创建通知服务扩展:
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()
}
}
}
- 为扩展配置 AppId 和配置文件。
丰富的通知正确到达:
但这是我面临的问题:
- didRecieve 未被调用。为此,我将 serviceExtension 进程附加到应用程序目标并运行该应用程序。
注意:通知到达后立即调用扩展,但未调用 didRecieve:
- 打开推送通知(带有视频附件)时,没有任何反应。理想情况下应该播放它。
- 如果我必须打开视频并播放它,我是否必须明确做某事或扩展程序会处理?
有效载荷:
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;
}
我确实尝试浏览了以下帖子,但没有帮助:
iOS10 UNNotificationServiceExtension not called
NotificationServiceExtension not called
UNNotificationServiceExtension not working on iPhone 5 (iOS 10)
最佳答案
好消息!您的服务扩展确实被调用了——您通知中的图像就是证据。此处可能发生的情况是您无法使用您习惯于处理应用程序的工作流程来调试扩展。
调试通知扩展不像调试应用程序。扩展是应用程序外部 iOS 进程的插件。仅仅设置断点并不是调试它们的可靠方法。相反:
调试通知服务扩展
当通知被传递时,服务扩展应该在调试器中启动。服务扩展仅与远程(推送)通知相关,因此您需要一个设备来解决它们。
调试通知内容扩展 至少有两种方法。上面显示的服务扩展步骤也适用于内容扩展。第二种方法更熟悉但不太可靠。
- 使用工具栏在 Xcode 中选择扩展方案
- 在“产品”菜单中,选择编辑方案...
- 将Executable 设置为父应用程序。
- 在内容扩展中设置断点。
- 现在构建并运行您的扩展程序。它将启动父应用程序。
- 触发将导致加载内容扩展的通知。
值得注意的是,使用日志记录框架添加日志记录对于调试和故障排除也非常有用。
为什么视频可能无法播放
iOS 限制了可以在通知中显示的内容的大小。 UNNotificationAttachment 的文档中对此进行了描述.对于视频,它通常是 50Mb。确保您的视频尽可能小(以字节为单位),当然还要提供适合其播放设备大小的视频。不要尝试在 400 点宽的通知中播放 1080p 视频!
在实践中,使用 HLS 而不是下载视频并在内容扩展中呈现它几乎总是更好。
您的代码中可能有问题的另一件事是您分配给附件的标识符。标识符应该唯一。通常这将是一个反向域表示法字符串,例如您的包 ID 后跟一个 UUID 字符串。您还可以使用内容的原始 URL,后跟 UUID 字符串。如果您提供一个空字符串,iOS 将为您创建一个唯一标识符。 具有非唯一标识符(用于通知、附件等)的用户通知框架往往会导致难以追踪框架内的问题。例如,这可能会导致连接的 watchOS 设备崩溃。
如果您想为您的视频实现“自动播放”——从您的问题中不清楚您所描述的是什么——您将需要在内容扩展中实现您自己的播放器功能。
如果您打算这样做,HLS 是在通知中显示视频的首选方式。它通常使用更少的 RAM,提供更好的用户体验并且往往更稳定。
关于ios - UNNotificationServiceExtension didReceive 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51722109/