2

I have a question about Notification Service Extension Lifecycle, I have searched but I cant find a suitable answer for my question. The question is what happen when new notification come when old notification is in processing (not called contentComplete yet)?

From my testing, it will end the old process and start new processing with newer notification, and user will miss old notification, with all data that is not saved yet. Is that true?

  • I haven't had a chance to test this. Knowing that the OS gives you 30 seconds before it kills your service extension I'm not certain if my answer is correct. You can easily test this by making your phone's network slow and giving it a big image to download followed by a tiny image to download and assess the behavior. – mfaani Jul 13 '20 at 15:59
  • I know about 30 seconds limit, but what I'm asking is different, please read my question again. – Tiến Nguyễn Hữu Jul 15 '20 at 06:37
  • I just noticed something. When I was trying to use Xcode's attach to process, I saw that I have TWO processes ongoing for my Notification Service Extension. So I'm now guessing that the OS spins off a new process – mfaani Mar 11 '21 at 15:31
  • @TiếnNguyễnHữu - Did you ever make out what's the exact case?? I mean the NSE's behaviour when old notification is still processing(contentHandler isn't called yet) and we receive a new notification? – user2606782 Mar 30 '21 at 08:05
  • @user2606782 as I said on my post, it will end old process and start new process with new notification, but I'm not sure cause there's no document about it. – Tiến Nguyễn Hữu Apr 02 '21 at 07:33

1 Answers1

2

I'm not absolutely positive but based on Signal's notification service extension code found here, what you're saying is correct. The way they solve it is as such:

// 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. 

Make sure you see the rest of their code

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • When debugging I recall sometimes in Xcode I had 2 distinct process IDs that I was able to attach onto. This makes me think that iOS sometimes fires up a new process. I don’t know when though – mfaani May 22 '21 at 00:12