如何确定 iOS 应用通知服务扩展进程即将被终止?

huangapple go评论59阅读模式
英文:

How to determine when iOS app notification service extension process is about to be terminated?

问题

通知服务扩展(NSE)允许在 iOS 应用程序中在向用户显示通知之前处理通知(例如,用于消息解密):

class NotificationService: UNNotificationServiceExtension {
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        // 当准备就绪时,在此处调用内容处理程序
    }

    override func serviceExtensionTimeWillExpire() {
        // 在此处使用最佳尝试调用内容处理程序
    }
}

据我了解,对于每个通知,都会创建一个新的 NotificationService 实例,但所有这些实例可以在同一个进程中,可能共享对数据库和其他资源的访问权限 - iOS 不会为每个新通知启动新的 NSE 进程。

在某些情况下,iOS 会终止 NSE 进程,如果数据库连接没有正确释放,将导致 0xdead10cc 退出代码的异常("死锁"),这对用户来说会显示为关于后台应用程序崩溃的警报。

是否有一种方法可以确定 iOS 即将终止 NSE 进程以释放资源?

英文:

Notification service extension (NSE) allows to process notifications in iOS app before showing them to the user (e.g., for message decryption):

class NotificationService: UNNotificationServiceExtension {
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        // call content handler here when ready
    }

    override func serviceExtensionTimeWillExpire() {
        // call content handler here with the best attempt
    }
}

As I understand, for each notification a new instance of NotificationService is created, but all these instances can be in the same process that may share access to the database and other resources - iOS doesn't start a new NSE process for each new notification.

At some point iOS kills NSE process, and if database connection is not properly released, it results in the exception with 0xdead10cc exit code ("dead lock") that is visible to the users as an alert about the crash of the app in the background.

Is there a way to determine when iOS is about to kill NSE process to release the resources?

答案1

得分: 1

从你的描述来看,你好像认为可以同时存在多个 NSE 实例?实际情况并非如此,如果手机同时接收到多个推送,操作系统会将它们放入队列中,然后启动和关闭,启动和关闭等待。

当你调用 contentHandler() 来显示通知时,扩展程序将被终止,或者如果你不调用它,那么大约在 30 秒后,操作系统会调用 serviceExtensionTimeWillExpire()。在这两种情况下,扩展程序都将被终止(如果在它运行时手机接收到另一个推送,那么它将被再次启动)。

英文:

From your description, you make it sound as if you are thinking there can be several instances of the NSE active at once? That's not what happens, if the handset receives multiple pushes simultaneously, the OS keeps them in a queue and launches then closes, launches then closes, etc. the extension.

The extension gets terminated when you call contentHandler() to display the notification, or if you don't call than then after about 30 seconds the OS calls serviceExtensionTimeWillExpire(). In both cases the extension will be terminated (and if while it was running, the phone had received another push, then it'll be launched again)

huangapple
  • 本文由 发表于 2023年7月20日 18:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定