简体   繁体   中英

iOS Push Notifications handle

Looking for a good practice regarding push notifications handle. currently in my app i'm handling the push notifications in the didFinishLaunchingWithOptions and in the didReceiveRemoteNotification delegates. I noticed that both handling logics are "firing" when i receive a push notifications when the app is "Dead". My remote notifications flag in Background Modes is ON in my app. Is there a good practice for handling this scenario ? why am i getting the push data in the didFinishLaunchingWithOptions(launchOptions) and didReceiveRemoteNotification is getting called too ? to my knowledge the didReceiveRemoteNotification is not spoused to get called when the app is "Dead".

didReceiveRemoteNotification is called when the notification is received and when you open the app from the notification. Background download will NOT be triggered if your app is dead and not open at all. What you are experiencing is didReceiveRemoteNotification being called because you tapped on the notification. What you need to do is add some logic to that method to see what state the app is in and deal with your notification there.

ie :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{


    if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@“inactivePush" object:nil];


    }

    else if([UIApplication sharedApplication].applicationState==UIApplicationStateActive){



        [[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];


    }

    //When the app is in the background
    else {



    }//End background

}

}

The first case is for if the app is inactive (opened from a push), the second is for when the app is actually open and the third is for when the app is in the background. I'm using NSNotifcation just as an example but you can use any handling code you want. The reason I added the final if case is because you can put your background/download code in there and this way it won't download things twice ie if you receive the notification and then tap on it.

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