Notification Service Extension usage time

Hello all. I noticed, that NSE living more than 30 seconds ( that described in doc ). When app receive notification, it created process NSE, and send notification to didReceive function, after this, app have 30 seconds to call contentHandler closure, after contentHandler is called, I expected that NSE process is killed, but it's not. If app using singletons in NSE, they won't dealloc after contentHandler is called, so, after new notification received, singletons still alive. Does it legal to not drop connection to websocket after contentHandler closure get called? For example, notification received, NSE process is loaded, websocket manager signleton is initialzied and started session, after few seconds contentHandler closure get called, so, system won't kill NSE because of 30 seconds timer, and my web socket connection will alive so long as possible, so, I not need to open it each 30 seconds, is that legal or not?)

  • I've not noticed them living longer than 30 seconds. How are you confirming that? If you have the Xcode debugger attached that might affect the lifetime

  • @mungbeans yes, I received notification, after that web socket get ( from singleton class ) so, after contentHandler is get called, NSE process is still alive, and singleton also continue exist in memory, so, next time when you receive new notification, singleton won't init again, and same instance will be used. It was tested on real device, my NSE process ( and socket connection ) alive about 10 minutes, I think it can alive more, but as I understand from answer, system can kill it anytime

Add a Comment

Replies

Once the contentHandler is closed, the system may decide that it is more efficient to keep the NSE process in memory and re-use the same process instead of terminating and reloading it for the next notification. The system will dynamically decide whether it is appropriate to handle the NSE one way or another.

So, yes, sometimes you may find that your singletons, static or class variables are still valid and containing data from the last time the NSE was run. Your code should be aware of this possibility and handle it as necessary.

This does not mean that your NSE will execute indefinitely after the contentHandler is called. Once this is called, your section of the NSE process exits, and the process will no longer get CPU time. Also, there is no guarantee that the NSE session will continue for any given instance. This is completely up to the system and cannot be influenced by anything you do in your code. Rather, the only influence on the behavior would be to not call contentHandler in a timely fashion, then the process will definitely be terminated (and any modifications to the notification content up to that point will be discarded)

For the specific use case you presented, yes you will need to close and re-open your web sockets with every notification.

  • [@Gualtier Malde](https://developer.apple.com/forums/profile/Gualtier Malde) thx for answer. What if it doesn't matter for my case, that system can terminate NSE process? just imagine, web socket aliving 10 minutes, during this, I send/receive messages via socket, after 10 min, system decide to terminate NSE, okay, but I will just receive new PN and new NSE process will start again, and new socket connection get opened, so, my connection time won't limit with 30 sec, it will limit on system decision, that, I think, will be longer than 30 sec

Add a Comment