How handle silent pushnotification when application is on InActive mode? In Objective C

598 Views Asked by At

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.

2

There are 2 best solutions below

8
Krishna Datt Shukla On BEST ANSWER

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];
}
1
Pushkraj Lanjekar On

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.