獲取APNS內(nèi)容并打開指定的ViewController

今天我自己的項(xiàng)目也有這個(gè)需求,在stackoverflow上發(fā)現(xiàn)有這個(gè)問題,但他們的回答都比較零散,我就稍微全面的回答了這個(gè)問題,比較懶,這里就把我的答案貼出來,英文也很簡單,也不做翻譯了,大家不要嫌棄:


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~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容