2

Yes there are also similar questions available in stack, So far, I didn't found any proper concurrent answer from those questions.

How can i download any data or call web-api, When i receive silent push notifications ?

My Code is as below..

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

     NSDictionary *dicAPS = [userInfo objectForKey:@"aps"];

     if(application.applicationState == UIApplicationStateBackground) {

        // This is working..
        [self callWebService];
     }
     else if(application.applicationState == UIApplicationStateInactive)
     {
        // This is not working..
        [self callWebService];
     }
     else
     {
        // This is working..
        //Show an in-app banner
     }
}

Note :
1) From web side, I already added "content-available" as 1.
2) I already added below key in Plist.

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

Hopefully, I'll get new hope from your answer.

Regards.

nynohu
  • 1,628
  • 12
  • 12
Sonu
  • 937
  • 1
  • 10
  • 39
  • you can also manage by payload by setting sound value ? – Shubham Dec 21 '16 at 06:22
  • @Shubham-Systematix Problem is how to call web api or execute any code when app is inactive. – Sonu Dec 21 '16 at 06:24
  • for this we need to set background mode but it's also working in 10 min. slot Also, Please check my below answer – Shubham Dec 21 '16 at 06:26
  • I checked whatsapp and other app, They managed received receipt based on Silent push notification and there is no any limit for 10 minutes. – Sonu Dec 21 '16 at 06:37
  • Please check my comment along with answer. may be it'll help you. – Shubham Dec 21 '16 at 07:13

2 Answers2

1

You are handling it in wrong way.

"didReceiveRemoteNotification" doesn't be call if your app is in InActive State.

Use the following code in your "didFinishLaunchingWithOptions" method in 'AppDelegate' to handle this State.

//Handling PUSH NOtIFICATIONs when app is killed

NSDictionary *pushDic = [[NSDictionary alloc]init];
pushDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];

if (pushDic)
{
    [self callWebService];
}
  • Is it execute code at the same time when receive push notification or at time when user will open app ? – Sonu Dec 21 '16 at 07:44
  • It will be executed when user will tap on banner. – Krishna Datt Shukla Dec 21 '16 at 07:45
  • I want to execute immediately when receive notification. – Sonu Dec 21 '16 at 08:41
  • You can't execute a piece of code just on receiving notification when your App is in InActive State. You must need to tap on banner to execute the code. – Krishna Datt Shukla Dec 21 '16 at 09:01
  • So, How its possible with Whatsapp ? Any idea ? Whatsapp is sending received receipt when it's receive any push notification. – Sonu Dec 21 '16 at 09:32
  • 1
    They Use Voip Push to achieve this. Voip Push is very fast and can invoke your app from InActive State and thus you can execute any code just after receiving Voip Push. – Krishna Datt Shukla Dec 21 '16 at 09:58
  • @ Krishna Datt Shukla : Thanks, For giving me exact direction. I've implemented VoIP push, It's working amazing now. I'm going to accept your answer based on your proper direction. Please edit your answer and add your suggestion in that answer so, other developers can get immediately help. – Sonu Dec 22 '16 at 09:50
  • @KrishnaDattShukla "didReceiveRemoteNotification" will be call if your app is in InActive State when silent push notification come. – Yogendra Patel Sep 04 '18 at 08:48
0

You can't handle any kind of remote or local notification if your app is in INACTIVE mode. App moves to INACTIVE when any other comes into picture like Call, Message or any other.

If you wan to do something needful, you can handle it in applicationDidBecomeActive by the help of any web service.

Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
  • Whatspp and manyother application working on similar mechanism (Silent push notification). Even, Apple is not providing support on Inactive mode, Why there is enum for UIApplicationStateInactive ? – Sonu Dec 21 '16 at 07:19