系統(tǒng) API :iOS < 7淌山、 7 <= iOS < 10泼疑、 iOS >= 10
app 狀態(tài):后臺(tái)運(yùn)行退渗、 前臺(tái)運(yùn)行、 未啟動(dòng)
iOS 6 及以前接收到遠(yuǎn)程推送的代理
- application: didReceiveRemoteNotification;
- application: didReceiveRemoteNotification 官方文檔
If the app is running, the app calls this method to process incoming remote notifications. If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification.
Instead, your implementation of the application:willFinishLaunchingWithOptions:
or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.
如果應(yīng)用程序正在運(yùn)行,應(yīng)用程序調(diào)用這個(gè)方法來(lái)處理傳入的遠(yuǎn)程通知秒裕。
如果應(yīng)用程序在遠(yuǎn)程通知到達(dá)時(shí)沒有運(yùn)行,該方法會(huì)啟動(dòng)應(yīng)用程序,并提供啟動(dòng)信息几蜻。應(yīng)用程序不會(huì)調(diào)用這個(gè)方法來(lái)處理遠(yuǎn)程通知梭稚。這時(shí)你可以通過application:didFinishLaunchingWithOptions:方法來(lái)獲得推送信息絮吵。
雖然是iOS 6之前的蹬敲,但使用范圍是NS_DEPRECATED_IOS(3_0, 10_0)。只是iOS 7之后有了更好的選擇急波,所以這個(gè)就顯得有點(diǎn)雞肋了澄暮。如果要適配iOS 6及以前還需實(shí)現(xiàn)這個(gè)
iOS 7(包含) 到 iOS 10(不包含)新增代理阱扬。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"[XGDemo] receive slient Notification");
NSLog(@"[XGDemo] userinfo %@", userInfo);
if (application.applicationState == UIApplicationStateActive) {
// 前臺(tái)處理
}else {
[self pushToVCWithNotificationInfo:userInfo];
}
completionHandler(UIBackgroundFetchResultNewData);
}
- application:didReceiveRemoteNotification:fetchCompletionHandler 官方文檔
?Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification:method, which is called only when your app is running in the foreground,the system calls this method when your app is running in the foreground or background.
?If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.
application:didReceiveRemoteNotification:只有當(dāng)應(yīng)用程序在前臺(tái)運(yùn)行調(diào)用馍刮,而這個(gè)方法在前臺(tái)窃蹋、后臺(tái)、未啟動(dòng)都能調(diào)用碎乃。如果兩個(gè)代理方法都被實(shí)現(xiàn)了惠奸,系統(tǒng)將只調(diào)用application:didReceiveRemoteNotification:fetchCompletionHandler:
因?yàn)槲覀儜?yīng)用只適配iOS 8及以上佛南,所以我就沒有再實(shí)現(xiàn)- (void)application: didReceiveRemoteNotification ;畢竟有了更新的代理,更簡(jiǎn)單不用單獨(dú)處理應(yīng)用未啟動(dòng)的情況及穗。當(dāng)然也可以使用- application: didReceiveRemoteNotification;
iOS 10及以上新增代理
首先要在application:didFinishLaunchingWithOptions中加入
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
#endif
然后實(shí)現(xiàn)代理
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
NSLog(@"[XGDemo] click notification");
//收到推送的請(qǐng)求
UNNotificationRequest *request = response.notification.request;
//收到推送的內(nèi)容
UNNotificationContent *content = request.content;
//收到用戶的基本信息
NSDictionary *userInfo = content.userInfo;
[self pushToVCWithNotificationInfo:userInfo];
completionHandler();
}
- userNotificationCenter: willPresentNotification: withCompletionHandler; // App 在前臺(tái)彈通知需要調(diào)用這個(gè)接口
跳轉(zhuǎn)詳情頁(yè)
- (void)jumpWithNotificationInfo:(NSDictionary *)info {
// 如果為空就返回
if (!info) {
return;
}
// 獲取包含的信息
NSString *type = info[@"type"];
// 首先,到最外層
UIViewController *topView;
topView = [self topViewController]; // 當(dāng)前控制器
[topView.navigationController popToRootViewControllerAnimated:NO]; // 跳到一級(jí)界面
if ([type isEqualToString:@"commentpass"]) { // 足跡
self.mainVC.selectedIndex = 0; // 然后娃豹,選中第幾個(gè)模塊
DiscoveryDetialViewController *VC = [[DiscoveryDetialViewController alloc] init];
UINavigationController *nav = self.mainVC.childViewControllers[0];
VC.hidesBottomBarWhenPushed = YES;
[nav pushViewController:VC animated:YES];
}
}