系統(tǒng) API :iOS < 7、 7 <= iOS < 10、 iOS >= 10
app 狀態(tài):后臺運行、 前臺運行、 未啟動
iOS 6 及以前接收到遠程推送的代理
- 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īng)用程序調(diào)用這個方法來處理傳入的遠程通知。
如果應(yīng)用程序在遠程通知到達時沒有運行,該方法會啟動應(yīng)用程序,并提供啟動信息。應(yīng)用程序不會調(diào)用這個方法來處理遠程通知。這時你可以通過application:didFinishLaunchingWithOptions:方法來獲得推送信息。
雖然是iOS 6之前的,但使用范圍是NS_DEPRECATED_IOS(3_0, 10_0)。只是iOS 7之后有了更好的選擇,所以這個就顯得有點雞肋了。如果要適配iOS 6及以前還需實現(xiàn)這個
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) {
// 前臺處理
}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)用程序在前臺運行調(diào)用,而這個方法在前臺、后臺、未啟動都能調(diào)用。如果兩個代理方法都被實現(xiàn)了,系統(tǒng)將只調(diào)用application:didReceiveRemoteNotification:fetchCompletionHandler:
因為我們應(yīng)用只適配iOS 8及以上,所以我就沒有再實現(xiàn)- (void)application: didReceiveRemoteNotification ;畢竟有了更新的代理,更簡單不用單獨處理應(yī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
然后實現(xiàn)代理
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
NSLog(@"[XGDemo] click notification");
//收到推送的請求
UNNotificationRequest *request = response.notification.request;
//收到推送的內(nèi)容
UNNotificationContent *content = request.content;
//收到用戶的基本信息
NSDictionary *userInfo = content.userInfo;
[self pushToVCWithNotificationInfo:userInfo];
completionHandler();
}
- userNotificationCenter: willPresentNotification: withCompletionHandler; // App 在前臺彈通知需要調(diào)用這個接口
跳轉(zhuǎn)詳情頁
- (void)jumpWithNotificationInfo:(NSDictionary *)info {
// 如果為空就返回
if (!info) {
return;
}
// 獲取包含的信息
NSString *type = info[@"type"];
// 首先,到最外層
UIViewController *topView;
topView = [self topViewController]; // 當(dāng)前控制器
[topView.navigationController popToRootViewControllerAnimated:NO]; // 跳到一級界面
if ([type isEqualToString:@"commentpass"]) { // 足跡
self.mainVC.selectedIndex = 0; // 然后,選中第幾個模塊
DiscoveryDetialViewController *VC = [[DiscoveryDetialViewController alloc] init];
UINavigationController *nav = self.mainVC.childViewControllers[0];
VC.hidesBottomBarWhenPushed = YES;
[nav pushViewController:VC animated:YES];
}
}