0
votes
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary     *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSLog(@"PUSH NOTIFICATION %@",userInfo);


    if([userInfo[@"aps"][@"content-available"] intValue] == 1){

        NSLog(@"SILENT PUSH NOTIFICATION");

        //Here I'm inserting a flag value to my DB.

        completionHandler(UIBackgroundFetchResultNewData);

    }
    else{

        NSLog(@" GENERAL PUSH NOTIFICATION ");

        completionHandler(UIBackgroundFetchResultNoData);

    }
}

My silent notification payload is {
        aps =     {
            "content-available" = 1;
            sound = "";
        };
    }

To support I have added a key in info.plist in "Required background modes"

item 0 = App downloads content in response to push notifications

Also in capabilities section my Background Modes are ON with remote notification check mark is selected.

Now my question is when I run my app then I'm able to receive silent as well as general push notification and code is executing successfully in foreground mode but when I press home button (not forcefully quitting by swiping it away and also device's passcode lock is open) my app is going to background mode and then my code is not executing even I'm able to receive push notifications those are having alert and sound, but app is not launching in background mode.

I'm thinking that for foreground and for background mode whenever my app is receiving any push notification don't matter is it silent or general push notification my first log in the delegate method should have to print on the console i.e NSLog(@"PUSH NOTIFICATION %@",userInfo);

Please help I'm struggling with this since last 2-3 days.

My info.plist here

2
Your code should always call completionHandler, so you should move the completionHandler(UIBackgroundFetchResultNewData); outside of both if statements. You may choose to change the value you pass to the completionHandler based on the message you receive and whether there is new dataPaulw11
Thanks Paulw11 for your reply but when I receive push notification while app is in background then it must have to print NSLog(@"PUSH NOTIFICATION %@",userInfo); this log on console but it isn't printing any log.Harry
Does any one know cause of the my problem ?? Please help me out.Harry
Thanks guys for your valuable feedbacks, I got solution my problem was the device I'm using for testing is only not executing in background. Today I tested code in another device and its working fine. So now I have to figure out why this particular device is behaving like this and not executing code in background.Harry

2 Answers

0
votes

I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".

@Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.

I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.

My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.

Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.

0
votes

I think you need to test once with 'content-available' outside 'aps', like the following:

aps = {
            alert = "This is test alert";
            badge = 1;
       };
"content-available" = 1;