代码之家  ›  专栏  ›  技术社区  ›  Nitish

未通知ServiceExtension的未接收未调用

  •  0
  • Nitish  · 技术社区  · 7 年前

    我一步一步地去获取丰富的推送通知。它们在这里:

    1. 已使用plist创建通知服务扩展:

    enter image description here

    通知服务没有收到 :

    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()
                }
            }
        }
    
    1. 为扩展配置了AppId和provision配置文件。

    丰富的通知正确到达:

    enter image description here

    但我现在面临的问题是:

    1. 迪德吉耶夫没有接到电话 . 为此,我将serviceExtension进程附加到应用程序目标并运行该应用程序。
      注意 :通知到达时将立即调用扩展,但未调用didreive:

    enter image description here

    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;
    } 
    

    我试着浏览了下面的帖子,但没有帮助:

    iOS10 UNNotificationServiceExtension not called
    NotificationServiceExtension not called
    UNNotificationServiceExtension not working on iPhone 5 (iOS 10)

    1 回复  |  直到 7 年前
        1
  •  5
  •   quellish    7 年前

    好消息!您的服务扩展确实正在被调用-您通知上的图像就是这一点的证据。这里可能发生的情况是,您无法使用与应用程序一起使用的工作流调试扩展。

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

    调试通知服务扩展

    1. 从Xcode或设备启动应用程序
    2. 在Xcode中,选择 按名称附加到进程或PID。。。 调试 菜单 Xcode Debug menu, Attach To Process or PID By Name...
    3. 输入通知扩展名 Xcode Debug menu, Enter Process Name
    4. 触发通知(通过发送推送等)。

    当通知被传递时,服务扩展应该在调试器中启动。服务扩展只与远程(推送)通知相关,因此您需要一个设备来解决这些问题。

    调试通知内容扩展 至少有两种方法。上面显示的服务扩展步骤也适用于内容扩展。第二种方法更为常见,但不太可靠。

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

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

    为什么视频不能播放

    iOS限制了可以在通知中显示的内容的大小。这在 UNNotificationAttachment . 对于视频,一般为50Mb。请确保您的视频尽可能小,以字节为单位,当然,提供一个视频,其大小适合将要播放的设备。不要试图在400点宽的通知中播放1080p视频!

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

    代码中另一个可能有问题的地方是您正在分配给附件的标识符。标识符应该是 独特的 . 通常这是一个反向域表示字符串,比如bundle ID后面跟着一个UUID字符串。还可以使用内容的原始URL,后跟UUID字符串。如果提供空字符串,iOS将为您创建唯一标识符。 由于用户通知框架具有非唯一标识符(用于通知、附件等),往往会导致难以跟踪框架内的问题。例如,这可能导致连接的watchOS设备崩溃。

    如果你想实现“自动播放”你的视频-从你的问题不清楚你是在描述什么-你将需要实现你自己的播放器功能在一个内容扩展。

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

    推荐文章