iOS開(kāi)發(fā)中經(jīng)常會(huì)用到遠(yuǎn)程推送,點(diǎn)擊推送的消息進(jìn)入指定的頁(yè)面更是一項(xiàng)基本功能搔体,本篇文章將介紹一下如何點(diǎn)擊消息進(jìn)入指定頁(yè)面行贪。
iOS點(diǎn)擊消息進(jìn)入app將分三種情況:app在后臺(tái)運(yùn)行茸塞,app在前臺(tái)運(yùn)行买鸽,app未啟動(dòng)。不同情況會(huì)走不同的方法预愤,其中app在后臺(tái)運(yùn)行和前臺(tái)運(yùn)行會(huì)走如下方法:
其中沟于,iOS7之前會(huì)走如下方法:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// App 收到推送的通知
NSLog(@"********** ios7.0之前 **********");
// 應(yīng)用在前臺(tái) 或者后臺(tái)開(kāi)啟狀態(tài)下,不跳轉(zhuǎn)頁(yè)面植康,讓用戶選擇旷太。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"提示" message:userInfo[@"aps"][@"alert"][@"body"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即查看", nil];
[alertView show];
}
else//殺死狀態(tài)下,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁(yè)面销睁。
{
TakeReceiptsListViewController *VC = [TakeReceiptsListViewController new];
VC.enterStyle = NO;
UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:VC];
[self.window.rootViewController presentViewController:na animated:YES completion:nil];
}
}
iOS7之后會(huì)走下面的方法:
#pragma mark - 此方法是 用戶點(diǎn)擊了通知供璧,應(yīng)用在前臺(tái) 或者開(kāi)啟后臺(tái)并且應(yīng)用在后臺(tái) 時(shí)調(diào)起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
// 打印到日志 textView 中
NSLog(@"********** iOS7.0之后 background **********");
// 應(yīng)用在前臺(tái) 或者后臺(tái)開(kāi)啟狀態(tài)下,不跳轉(zhuǎn)頁(yè)面冻记,讓用戶選擇睡毒。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"提示" message:userInfo[@"aps"][@"alert"][@"body"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即查看", nil];
[alertView show];
}
else//殺死狀態(tài)下,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁(yè)面冗栗。
{
TakeReceiptsListViewController *VC = [TakeReceiptsListViewController new];
VC.enterStyle = NO;
UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:VC];
[self.window.rootViewController presentViewController:na animated:YES completion:nil];
}
}
iOS10 Apple更新了推送方法演顾,收到通知點(diǎn)擊觸發(fā)的方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler已被棄用供搀,使用新的方法如下:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
completionHandler(UIBackgroundFetchResultNewData);
NSDictionary * userInfo = response.notification.request.content.userInfo;
[self pushToNewsDetailViewController:userInfo];
}
如果app未運(yùn)行的話會(huì)先走走如下方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
然后再走:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;