简体   繁体   中英

How to make a server call on receiving push notification in swift?

I am developing a messenger app like whatsapp, it has to notify the server that the message is received, I am sending the status to server when didReceiveRemoteNotification is called. But when the app is not in foreground, it is calling only when the user taps on the notification. How to make server calls even when the app is in background or not running?

To receive a remote notification when your app is in background or suspended mode first your server should send special parameter in notification payload called content-available = 1. to learn more about that look the documentation: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW5

Also turn on this switch in the project settings: 在此输入图像描述

With this parameters when your app will receive a push notification it will be waken up for some small time to background mode and call didReceiveRemoteNotification . Don't forget to call completionHandler(<some enum parameter>) in the end of your task, or your process can be killed by iOS unexpectedly, for example when you're trying to perform async server requset. So when your async method completes call completionHandler closure argument of didReceiveRemoteNotification .

Actually there is no managed code can be run when your app is completely thrown away from the app switcher. As far as I know to perform that you should use VOIP push messages - but that kind of messages is only for apps with voice calls, like Skype or WhatsApp. This kind of apps has some code to perform even if they are completely turned off. iOS gives this ability to them to prepare to voice call. That's why all these messengers use Voice calls - to make instant delivery status for example. If you want that kind of interaction you should google more about VOIP. But your app should really have some voice calls function or it will be rejected by the app store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM