【问题标题】:UNNotificationServiceExtension's didRecieve not calledUNNotificationServiceExtension didReceive 未调用
【发布时间】:2019-01-14 06:26:04
【问题描述】:

我一步一步地获得丰富的推送通知。他们在这里:

  1. 使用 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()
            }
        }
    }
  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;
} 

我确实尝试过浏览以下帖子,但没有帮助:

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

【问题讨论】:

    标签: ios swift apple-push-notifications unnotificationserviceextension


    【解决方案1】:

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

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

    调试通知服务扩展

    1. 从 Xcode 或设备启动应用程序
    2. 在 Xcode 中,从 Debug 菜单中选择 Attach To Process or PID By Name...
    3. 输入您的通知扩展名
    4. 触发通知(通过发送推送等)。

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

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

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

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

    为什么视频可能无法播放

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

    在实践中,使用 HLS 几乎总是比下载视频更好,并将其呈现在内容扩展中。

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

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

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

    【讨论】:

    • 感谢您的精彩解释。其实这是我的愚蠢。一切正常。我没有长按打开视频的通知。我真的很愚蠢,对 :D 但我正在按照您提到的所有步骤进行操作。
    • 如果扩展永远不会被触发怎么办?我正在发送“可变内容”:1并且它有一个警报,就像苹果文档状态developer.apple.com/documentation/usernotifications/…一样,但是当我尝试附加时,我只看到消息“PushService Waiting to attach”我知道通知正在接收到设备,当我切换徽章计数时,我看到它在设备上得到更新......
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多