收到消息數(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)程推送消息分為兩步
- 先調(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)行處理。