简体   繁体   中英

Execute few lines of code when received silent push notification and app is running in background, ios

-(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 ie NSLog(@"PUSH NOTIFICATION %@",userInfo);

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

My info.plist 这里

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM