Is it possible to send a request when the phone is in sleep mode? A push notification arrives, after that, you must send a request to the server, but when the phone is in sleep mode, the request is not sent.

Is it possible to fix this?

    1 answer 1

    It seems that you need to set the right background mode

    When the device is in sleep mode, applications are frozen and cannot perform any operations, but there are a few exceptions:

    1. Playing audio
    2. Receive updated geolocation data (from GPS module)
    3. Provision of VoIP service (telephony)
    4. Processing downloads from Newsstand (magazine store)
    5. Interaction with external devices (connected via the headphone port or Lightning)
    6. Work with Bluetooth (as host or as server)
    7. Perform data loading in background
    8. Handling Push Notifications

    Actually, you need to show the system that the application wants to work in mode number 8. To do this, set the remote-notification value in the UIBackgroundModes parameter in the project Info.plist file.

    Next you need to implement the method

     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler { ... } 

    in AppDelegate . However, according to the documentation, all this is intended for downloading content when a push appears, so that the user, after opening the notification in the application, does not wait for the data to be downloaded, but immediately sees the information. To do this, the message must be set the content-available flag, otherwise the miracle will not happen.

    • remote-notification is specified, the notification content is processed, but there is no internet connection. - Vitali Eller
    • The documentation insist on using NSURLSession , can try through it? Generally, is there any error or is the object not created to send the data? Or the handler is not called at all? - Andrey Chevozerov
    • The handler is called, the object is created, everything goes well, but after sending there is no response from the server. Only local actions occur, on iOS 8 and under, even local actions are not found, only push is processed and that's it. - Vitali Eller
    • And there is an opportunity to look at the server that comes there? Or try using a sniffer to intercept a request, but this is not the easiest way ... - Andrey Chevozerov
    • The message does not reach the server, although the device says it has sent. - Vitali Eller