【问题标题】:Notification Service Extension Lifecycle通知服务扩展生命周期
【发布时间】:2020-10-15 09:57:23
【问题描述】:

我有一个关于通知服务扩展生命周期的问题,我已经搜索过,但找不到适合我的问题的答案。问题是当旧通知正在处理(尚未称为 contentComplete)时出现新通知时会发生什么?

根据我的测试,它将结束旧进程并使用更新的通知开始新的处理,用户将错过旧通知,所有尚未保存的数据。这是真的吗?

【问题讨论】:

  • 我还没有机会测试这个。知道操作系统会在终止您的服务扩展之前给您 30 秒,我不确定我的答案是否正确。您可以通过让手机的网络变慢并给它一个大图像来下载,然后是一个小图像来下载和评估行为来轻松测试这一点。
  • 我知道大约 30 秒的限制,但我的要求不同,请再次阅读我的问题。
  • 我刚刚注意到一些事情。当我尝试使用 Xcode 的附加到进程时,我看到我的通知服务扩展有两个进程正在进行。所以我现在猜测操作系统会衍生出一个新进程
  • @TiếnNguyễnHữu - 你有没有弄清楚确切的情况是什么?我的意思是当旧通知仍在处理(尚未调用 contentHandler)并且我们收到新通知时 NSE 的行为?
  • @user2606782 正如我在帖子中所说,它将结束旧流程并通过新通知启动新流程,但我不确定,因为没有关于它的文档。

标签: ios unnotificationserviceextension


【解决方案1】:

我不是绝对肯定的,但根据找到 here 的 Signal 通知服务扩展代码,您所说的是正确的。他们的解决方法是这样的:

// The lifecycle of the NSE looks something like the following:
//  1)  App receives notification
//  2)  System creates an instance of the extension class
//      and calls this method in the background
//  3)  Extension processes messages / displays whatever
//      notifications it needs to
//  4)  Extension notifies its work is complete by calling
//      the contentHandler
//  5)  If the extension takes too long to perform its work
//      (more than 30s), it will be notified and immediately
//      terminated
//
// Note that the NSE does *not* always spawn a new process to
// handle a new notification and will also try and process notifications
// in parallel. `didReceive` could be called twice for the same process,
// but will always be called on different threads. To deal with this we
// ensure that we only do setup *once* per process and we dispatch to
// the main queue to make sure the calls to the message fetcher job
// run serially. 

确保您看到他们的其余代码

【讨论】:

  • 在调试时我记得有时在 Xcode 中我有 2 个不同的进程 ID,我可以附加到这些 ID 上。这让我觉得 iOS 有时会启动一个新进程。我不知道什么时候
猜你喜欢
  • 2015-09-24
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多