Search code examples
iosobjective-cpush-notificationsilentpush

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


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.


Solution

  • 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];
    }