iOS9和iOS10推送區(qū)別

收到消息數(shù)據(jù)格式區(qū)別

//iOS9消息格式如下
 //{
 //    aps =     {
 //        alert = "\U65b0\U6d88\U606f\U6765\U4e86\U3002";
 //        sound = default;
 //    };
 //    d = us60289150408047357911;
 //    p = 0;
 //    pustId = 45267;
 //    pustType = 5;
 //}
 
 iOS10消息格式如下
 //{
 //    aps =     {
 //        alert =         {
 //            body = sdfsfsdf;
 //            subtitle = xxxx;
 //            title = xxx;
 //        };
 //        sound = default;
 //    };
 //    d = us29253150408072030511;
 //    p = 0;
 //    pustId = 45267;
 //    pustType = 5;
 //}

要根據(jù)具體數(shù)據(jù)格式進(jìn)行解析,以適配iOS10+。

收到遠(yuǎn)程推送消息幾種情況處理

前臺(tái)接收消息情況

前臺(tái)接收消息即app處于打開(kāi)正在運(yùn)行的時(shí)候,此時(shí),app收到遠(yuǎn)程推送時(shí)候
在iOS9系統(tǒng)下調(diào)用

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

iOS10系統(tǒng)下調(diào)用

//iOS10新增:處理前臺(tái)收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //應(yīng)用處于前臺(tái)時(shí)的遠(yuǎn)程推送接受
    }else{
        //應(yīng)用處于前臺(tái)時(shí)的本地推送接受
    }
    //當(dāng)應(yīng)用處于前臺(tái)時(shí)提示設(shè)置,需要哪個(gè)可以設(shè)置哪一個(gè)
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

后臺(tái)接收消息情況

后臺(tái)接收消息,指app按下home鍵進(jìn)入后臺(tái)或者設(shè)備進(jìn)入鎖定狀態(tài),此時(shí)接收到遠(yuǎn)程推送消息
iOS9系統(tǒng)下調(diào)用:

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

iOS10系統(tǒng)下調(diào)用:

//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //應(yīng)用處于后臺(tái)時(shí)的遠(yuǎn)程推送接受       
    }else{
        //應(yīng)用處于后臺(tái)時(shí)的本地推送接受
    }
}

app未啟動(dòng)的情況

在iOS9系統(tǒng)下,app接收遠(yuǎn)程推送消息調(diào)用Appdelegate中的

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

其中launchOptions中可以獲取推送消息內(nèi)容,然后根據(jù)消息內(nèi)容進(jìn)行處理。

在iOS10系統(tǒng)下,app接收遠(yuǎn)程推送消息分為兩步

  1. 先調(diào)用
//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //應(yīng)用處于后臺(tái)時(shí)的遠(yuǎn)程推送接受
        //必須加這句代碼       
    }else{
        //應(yīng)用處于后臺(tái)時(shí)的本地推送接受
    }
}

2.然后走AppDelegate中的

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

其中,launchOption中包含推送消息內(nèi)容。

總結(jié)

  • iOS9和iOS10由于收到消息內(nèi)容格式不一樣需要分開(kāi)區(qū)別處理。

  • 在iOS9系統(tǒng)下:app啟動(dòng)情況(包括前臺(tái)和后臺(tái))都在

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

中處理推送消息,在app未啟動(dòng)情況下,在AppDelegate中的

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

處理遠(yuǎn)程消息。

  • 在iOS10系統(tǒng)下:app在前臺(tái)收到遠(yuǎn)程推送,在
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

方法中處理推送消息。
在后臺(tái)或者未啟動(dòng)收到遠(yuǎn)程推送消息,在

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

方法中處理推送消息。

注意

  • 不論iOS9或者iOS10,launchOptions一般就是消息推送的內(nèi)容。(如果是點(diǎn)擊3D Touch點(diǎn)擊iCon進(jìn)入App,這個(gè)launchOptions就是3D Touch的相關(guān)數(shù)據(jù)字典)。如果是iOS10,最好判斷下,不處理,因?yàn)閕OS10的代理方法userNotificationCenter: didReceiveNotificationResponse: withCompletionHandler:會(huì)處理。

普及一下,launchOptions參數(shù):
1.App直接啟動(dòng),launchOptions為null
2.從他應(yīng)用程序通過(guò)openURL:啟動(dòng),則UIApplicationLaunchOptionsURLKey
對(duì)應(yīng)的對(duì)象為啟動(dòng)URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey
對(duì)應(yīng)啟動(dòng)的源應(yīng)用程序的bundle ID (NSString);
3.從本地通知啟動(dòng),則UIApplicationLaunchOptionsLocalNotificationKey對(duì)應(yīng)的
是本地通知對(duì)象(UILocalNotification);
4.從遠(yuǎn)程通知啟動(dòng),則UIApplicationLaunchOptionsRemoteNotificationKey
對(duì)應(yīng)的遠(yuǎn)程消息通知信息userInfo(NSDictionary);
5.從點(diǎn)擊3D Touch iCon啟動(dòng),則UIApplicationLaunchOptionsShortcutItemKey
對(duì)應(yīng)的是點(diǎn)擊的iCon的信息。

  • 在前臺(tái)收到推送消息,在iOS10系統(tǒng)的設(shè)備推送消息欄會(huì)收到一條推送消息同時(shí)應(yīng)用中也會(huì)進(jìn)行推送消息處理,也就是說(shuō)你點(diǎn)擊消息欄,還會(huì)進(jìn)行一次推送消息處理。而iOS9系統(tǒng)下消息欄不會(huì)有推送消息記錄,只有應(yīng)用中會(huì)收到推送消息進(jìn)行處理。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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