简体   繁体   中英

how to perform action on receive push notification when app is in background?

My app using remote notification when we want to inform user that new data is available for his application ,In code I had written updateData method (which pulls data from server) in didReceiveRemoteNotification method of appDelegate. Now it works fine if my application is active, but not for inactive mode.Is I am wrong at somewhere ? what will be solution for that ? Thanks in advance.

I know of no way to handle push notifications even if your app is not in active state (And I think that's exactly the it should be)

You should check for PNS in your AppDelegate's didFinishLaunch as well

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    //...
    //If Push Notification
    NSDictionary *pnsDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(pnsDict != nil){
        DDLogInfo(@"PNS");
        [self handlePushNotification:pnsDict]; 
    }
    //..
}

This way you can be sure that you catch all remote notifications.

Be aware, that your app should not depend on pns . PNS maybe fail to be delivered or the user can turn them off. The application should always work with pns enabled as well as pns disabled in the same way.

For more information read the Apple PNS Guide

That's how notifications are supposed to work: if the app is the foreground app, it receives the notification directly; otherwise the user sees a message pop up asking if they want to activate the app. It's described clearly in the docs . You can't avoid that dialog if you want to use APNS.

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