收到消息數(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)程推送消息幾種情況處理
前臺接收消息情況
前臺接收消息即app處于打開正在運(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新增:處理前臺收到通知的代理方法
-(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)用處于前臺時(shí)的遠(yuǎn)程推送接受
}else{
//應(yīng)用處于前臺時(shí)的本地推送接受
}
//當(dāng)應(yīng)用處于前臺時(shí)提示設(shè)置朽色,需要哪個(gè)可以設(shè)置哪一個(gè)
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
后臺接收消息情況
后臺接收消息,指app按下home鍵進(jìn)入后臺或者設(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新增:處理后臺點(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)用處于后臺時(shí)的遠(yuǎn)程推送接受
}else{
//應(yīng)用處于后臺時(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新增:處理后臺點(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)用處于后臺時(shí)的遠(yuǎn)程推送接受
//必須加這句代碼
}else{
//應(yīng)用處于后臺時(shí)的本地推送接受
}
}
2.然后走AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
其中,launchOption中包含推送消息內(nèi)容赵讯。
總結(jié)
iOS9和iOS10由于收到消息內(nèi)容格式不一樣需要分開區(qū)別處理盈咳。
在iOS9系統(tǒng)下:app啟動(dòng)情況(包括前臺和后臺)都在
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
中處理推送消息,在app未啟動(dòng)情況下瘦癌,在AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
處理遠(yuǎn)程消息猪贪。
- 在iOS10系統(tǒng)下:app在前臺收到遠(yuǎn)程推送跷敬,在
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
方法中處理推送消息讯私。
在后臺或者未啟動(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:
會處理。
普及一下悔详,launchOptions參數(shù):
1.App直接啟動(dòng)镊屎,launchOptions
為null
2.從他應(yīng)用程序通過openURL:啟動(dòng),則UIApplicationLaunchOptionsURLKey
對應(yīng)的對象為啟動(dòng)URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey
對應(yīng)啟動(dòng)的源應(yīng)用程序的bundle ID (NSString)茄螃;
3.從本地通知啟動(dòng)缝驳,則UIApplicationLaunchOptionsLocalNotificationKey
對應(yīng)的
是本地通知對象(UILocalNotification
);
4.從遠(yuǎn)程通知啟動(dòng)用狱,則UIApplicationLaunchOptionsRemoteNotificationKey
對應(yīng)的遠(yuǎn)程消息通知信息userInfo(NSDictionary)运怖;
5.從點(diǎn)擊3D Touch iCon啟動(dòng)摇展,則UIApplicationLaunchOptionsShortcutItemKey
對應(yīng)的是點(diǎn)擊的iCon的信息吗购。
- 在前臺收到推送消息,在iOS10系統(tǒng)的設(shè)備推送消息欄會收到一條推送消息同時(shí)應(yīng)用中也會進(jìn)行推送消息處理砸狞,也就是說你點(diǎn)擊消息欄捻勉,還會進(jìn)行一次推送消息處理。而iOS9系統(tǒng)下消息欄不會有推送消息記錄刀森,只有應(yīng)用中會收到推送消息進(jìn)行處理踱启。