获取APNS内容并打开指定的ViewController

今天我自己的项目也有这个需求,在stackoverflow上发现有这个问题,但他们的回答都比较零散,我就稍微全面的回答了这个问题,比较懒,这里就把我的答案贴出来,英文也很简单,也不做翻译了,大家不要嫌弃:


There are two chances that you will access the notification data.
If you receive the notification when your app isn't on, then click the notification and you will get the notification data in the following function:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

use

launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]

to access the notification data and open the view controller you expected.

If you receive the notification when your app is on, but your app can be in background or foreground. If it is the former case, you will receive the notification in notification center, your app will invoke the following function after you click the notification:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

If it is the later case, you app will directly invoke the function before. And you can distinguish them using the flowing code:

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive){ 
    //notification is received when your app is in background 
    //open the view controller you expected
}else if(state == UIApplicationStateActive){ 
    //notification is received when your app is in foreground 
    //do nothing
}

sorry for my pool English, hope it helps~

你可能感兴趣的:(获取APNS内容并打开指定的ViewController)