未调用UNNotificationServiceExtension的didRecieve

Nit*_*ish 0 apple-push-notifications ios swift unnotificationserviceextension

我逐步移动以获得丰富的推送通知。他们来了 :

  1. 使用plist创建了Notification服务扩展:

在此处输入图片说明

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)
  1. 已配置的AppId和扩展配置文件。

丰富的通知正确发出:

在此处输入图片说明

但是,这里是我面临的问题:

  1. didRecieve没有被调用。为此,我将serviceExtension流程附加到应用程序目标并运行了该应用程序。
    注意:通知到达后立即调用扩展,但是didRecieve没有被调用:

在此处输入图片说明

  1. 打开推送通知(带有视频附件)时,什么也没有发生。理想情况下,它应该播放。
  2. 如果我必须打开视频并播放,是否必须明确地做点什么,否则扩展会解决吗?

有效负载

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)上不起作用

que*_*ish 5

好消息!实际上,您的服务扩展名已被调用-通知中的图像即证明了这一点。这里可能发生的情况是,您无法使用应用程序惯用的工作流来调试扩展。

调试通知扩展与调试应用程序不同。扩展程序是应用程序外部的iOS进程的插件。仅仅设置一个断点并不是调试它们的可靠方法。代替:

调试Notification Service扩展

  1. 从Xcode或设备启动应用
  2. 在Xcode中,从“ 调试”菜单中 选择“ 按名称附加到进程或PID ...Xcode调试菜单,按名称附加到进程或PID ...
  3. 输入您的通知扩展名 Xcode调试菜单,输入进程名称
  4. 触发通知(通过发送推送等)。

发出通知后,服务扩展应启动到调试器。服务扩展仅与远程(推送)通知相关,因此您将需要一台设备来对其进行故障排除。

调试通知内容扩展 至少有两种方法。上面显示的服务扩展步骤也适用于内容扩展。第二种方法比较熟悉,但可靠性较低。

  1. 使用工具栏在Xcode中选择扩展方案 Xcode方案工具栏
  2. 在产品菜单中,选择编辑方案... Xcode编辑方案
  3. 可执行文件设置为父应用程序。 Xcode可执行文件集
  4. 在内容扩展中设置一个断点。
  5. 现在构建并运行您的扩展程序。它将启动父应用程序。
  6. 触发将导致内容扩展加载的通知。

值得注意的是,使用日志记录框架添加日志记录对于调试和故障排除也非常有用。

为什么视频可能无法播放

iOS限制了可以在通知中显示的内容的大小。UNNotificationAttachment的文档中对此进行了描述。对于视频,通常为50Mb。确保您的视频尽可能小(以字节为单位),并且当然要提供适合于将要播放的设备的视频大小。不要尝试在400点宽的通知中播放1080p视频!

实际上,使用HLS而不是下载视频并将其显示在内容扩展中几乎总是更好的选择。

代码中可能会出现问题的另一件事是您分配给附件的标识符。标识符应该是唯一的。通常,这是一个反向域符号字符串,例如您的捆绑软件ID,后跟一个UUID字符串。您还可以使用内容的原始URL,后跟UUID字符串。如果您提供一个空字符串,iOS将为您创建一个唯一标识符。对于具有非唯一标识符(用于通知,附件等)的用户通知框架,往往会导致难以跟踪框架内部的问题。例如,这可能导致连接的watchOS设备崩溃。

如果您想为视频实现“自动播放”-从您的问题还不清楚这是您要描述的内容-您将需要在内容扩展中实现自己的播放器功能。

如果您要这样做,HLS是在通知中显示视频的首选方式。它通常使用较少的RAM,提供更好的用户体验,并趋于更稳定。