點(diǎn)擊 iOS 接收遠(yuǎn)程推送主要牽扯到的方法有以下五種
(1) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
(2) - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
(3) - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
(4) - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
(5) - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
- 會(huì)在app啟動(dòng)完成調(diào)用
launchOptions
保存了app啟動(dòng)的原因信息旺订,如果app是因?yàn)辄c(diǎn)擊通知欄啟動(dòng)的摆屯,可以在launchOptions
獲取到通知的具體內(nèi)容况既。 - 會(huì)在接收到通知的時(shí)候調(diào)用睛琳,在最新的iOS 10中已經(jīng)廢棄脖咐,建議不再使用断箫。
- 是在iOS 7之后新增的方法瓜浸,可以說是 2 的升級版本澳淑,如果app最低支持iOS 7的話可以不用添加 2了。
其中completionHandler
這個(gè)block可以填寫的參數(shù)UIBackgroundFetchResult
是一個(gè)枚舉值插佛。主要是用來在后臺(tái)狀態(tài)下進(jìn)行一些操作的杠巡,比如請求數(shù)據(jù),操作完成之后雇寇,必須通知系統(tǒng)獲取完成氢拥,可供選擇的結(jié)果有
typedef NS_ENUM(NSUInteger, UIBackgroundFetchResult) {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
}
分別表示獲取到了新數(shù)據(jù)(此時(shí)系統(tǒng)將對現(xiàn)在的UI狀態(tài)截圖并更新App Switcher中你的應(yīng)用的截屏)蚌铜,沒有新數(shù)據(jù),以及獲取失敗嫩海。不過以上操作的前提是已經(jīng)在Background Modes
里面勾選了Remote notifications
(推送喚醒)且推送的消息中包含content-available
字段冬殃。
- 是 iOS 10 新增的
UNUserNotificationCenterDelegate
代理方法,在 iOS 10的環(huán)境下叁怪,點(diǎn)擊通知欄都會(huì)調(diào)用這個(gè)方法造壮。 - 也是 iOS 10 新增的
UNUserNotificationCenterDelegate
代理方法,在iOS 10 以前骂束,如果應(yīng)用處于前臺(tái)狀態(tài)耳璧,接收到推送,通知欄是不會(huì)又任何提示的展箱,如果開發(fā)者需要展示通知旨枯,需要自己在 3 的方法中提取通知內(nèi)容展示。在iOS 10中如果開發(fā)者需要前臺(tái)展示通知混驰,可以再在這個(gè)方法中completionHandler
傳入相應(yīng)的參數(shù)攀隔。
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
總結(jié):(最低系統(tǒng)環(huán)境為iOS 7)
- 當(dāng)程序處于關(guān)閉狀態(tài)的時(shí)候收到推送消息,點(diǎn)擊應(yīng)用程序圖標(biāo)無法獲取推送消息, iOS 10 環(huán)境下栖榨,點(diǎn)擊通知欄會(huì)調(diào)用 方法1與4昆汹。非iOS 10的情況下,會(huì)調(diào)用方法1與3婴栽。
- 當(dāng)程序處于前臺(tái)狀態(tài)下收到推送消息满粗,iOS 10的環(huán)境下如果推送的消息中包含
content-available
字段的話執(zhí)行方法3與4,否則只執(zhí)行4。非iOS 10的情況下會(huì)執(zhí)行方法3愚争。 - 當(dāng)程序處于后臺(tái)收到推送消息映皆,如果已經(jīng)在
Background Modes
里面勾選了Remote notifications
(推送喚醒)且推送的消息中包含content-available
字段的話,都會(huì)執(zhí)行方法3轰枝。點(diǎn)擊通知欄 iOS 10會(huì)執(zhí)行方法4捅彻,非iOS10會(huì)執(zhí)行方法3。