通知服务扩展生命周期

时间:2020-06-25 02:28:23

标签: ios unnotificationserviceextension

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

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

1 个答案:

答案 0 :(得分:0)

我不是绝对肯定的,但是基于发现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. 

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