Reputation: 300
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?
Upvotes: 4
Views: 2689
Reputation: 36257
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
Alternate answer:
Also see here
What happens if multiple push notifications arrive?
iOS will launch the notification-service-extension upon receiving the first push notification. Subsequent push notifications are queued by the OS. After the app extension finishes processing the first notification (by invoking thecontentHandler
), then iOS will:
- display the first push notification
- dealloc the
UNNotificationServiceExtension
- Initialize a new
UNNotificationServiceExtension
instance- And invoke it's
didReceive(_:)
function with the next item in the queueNote that it does NOT create a new app extension process. It re-uses the existing process, and launches a new
UNNotificationServiceExtension
within it.
So one answer says they might spawn a new process while the other says they get queued onto the same process 🤷♂️
Upvotes: 4