What does completionHandler in userNotificationCenter actually do?

时间:2019-05-31 11:36:20

标签: ios apple-push-notifications unusernotificationcenter

I'm currently implementing push notifications and I was wondering, what does completionHandler in userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: actually do?

Apple's documentation says:

The block to execute when you have finished processing the user’s response. You must execute this block at some point after processing the user's response to let the system know that you are done. The block has no return value or parameters.

But this is very cryptic and doesn't actually answer my question. Does it kill the app because I "finished handling the action"? Does it not kill the app? What if I have some async calls (like replying to a message) and I call the completionHandler? For now it seems to be working but what if the connection is slow?

1 个答案:

答案 0 :(得分:2)

If your app is in background when it receives the push notification, calling the handler signify to iOS that your application no longer needs to be assigned CPU cycles for processing until you either receive another push notification or the user interacts with your app in some way.

If your app is in foreground and active, it should have no effect.

In any case, you should be a "good iOS citizen" and only do the strict minimum when receiving a push notification. Note that there are some background download services available if you need, which you can trigger when receiving the push notification.

And as the text says, you HAVE to call this completion handler.