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'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 `didReceive` 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 it will always be called on different threads. It may or may not be
// called on the same instance of `NotificationService` as a previous
// notification.
//
// We keep a global `environment` singleton to ensure that our app context,
// database, logging, etc. are only ever setup once per *process*
Make sure you see the rest of their code
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 🤷♂️
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With