基本步驟就不再說了,可以谷歌
遠程推送應用配置過程
一. 創建支持遠程推送功能的App ID
二. 創建推送證書(開發證書和發佈證書)和描述文件
三. 下載CER文件,並導入鑰匙串管理
四. 我們需要重新生成一下配置文件
下面開始就介紹,點擊推送的內容跳轉指定頁面
現在點擊推送消息,有兩種跳轉方式:
one.打開應用,跳轉到應用首頁;
默認的效果是點擊推送消息,會直接進入應用,什麼都不用設置,只要註冊極光應用就可以。可以參考官方文檔,寫的非常詳細,直接拷貝文檔代碼即可。
two.打開應用,跳轉到指定頁面。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
if (application.applicationState.applicationState == UIApplicationStateActive) {
//程序運行時收到通知,先彈出消息框
NSLog(@"程序在前臺");
[self popAlert:userInfo];
}
else{
//程序已經關閉或者在後臺運行
[self pushToViewControllerWhenClickPushMessageWith:userInfo];
//這裏也可以發送個通知,跳轉到指定頁面
// [self readNotificationVcWithUserInfo:userInfo];
}
[application setApplicationIconBadgeNumber:0];
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的請求
UNNotificationContent *content = request.content; // 收到推送的消息內容
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
//程序運行時收到通知,先彈出消息框
NSLog(@"程序在前臺");
[self popAlert:userInfo];
}
else{
//跳轉到指定頁面
[self pushToViewControllerWhenClickPushMessageWith:userInfo];
//這裏也可以發送個通知,跳轉到指定頁面
// [self readNotificationVcWithUserInfo:userInfo];
}
}
// 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
[[NSUserDefaults standardUserDefaults] setObject:@"Inactive" forKey:@"applicationState"];
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
//程序運行時收到通知,先彈出消息框
[self popAlert:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationState" object:@"0"];
}
else{
[self pushToViewControllerWhenClickPushMessageWith:userInfo];
//這裏也可以發送個通知,跳轉到指定頁面
// [self readNotificationVcWithUserInfo:userInfo];
}
}
completionHandler(); // 系統要求執行這個方法
}
#endif
#pragma mark -- 程序運行時收到通知
-(void)popAlert:(NSDictionary *)pushMessageDic{
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:@"" message:[[pushMessageDic objectForKey:@"aps"]objectForKey:@"alert"]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAction =
[UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self pushToViewControllerWhenClickPushMessageWith:pushMessageDic];
}];
UIAlertAction *cancelAction =
[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:confirmAction];
[alertController addAction:cancelAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
-(void)pushToViewControllerWhenClickPushMessageWith:(NSDictionary*)msgDic{
NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
if ([[msgDic objectForKey:@"type"] integerValue]==0){
// 跳轉到第一個tabbarItem,這時直接設置 UITabBarController的selectedIndex屬性就可以
self.tabController.selectedIndex = 0;
}else if ([[msgDic objectForKey:@"type"] integerValue]==1){
//跳轉到第二個tabbarItem
self.tabController.selectedIndex = 1;
}else if ([[msgDic objectForKey:@"type"] integerValue]==2){
//跳轉到第三個tabbarItem
self.tabController.selectedIndex = 2;
}else if ([[msgDic objectForKey:@"type"] integerValue]==3){
//詳情,這是從跳轉到第一個tabbarItem跳轉過去的,所以我們可以先讓tabController.selectedIndex =0;然後找到VC的nav。
self.tabController.selectedIndex =0;
MessageViewController * VC = [[MessageViewController alloc]init];
[VC setHidesBottomBarWhenPushed:YES];
//因爲我用了三方全屏側滑手勢,所以要找的是第一個tabbarController中的viewController的JTNavigationController ,接着再找JTNavigationController 裏面的jt_viewControllers.lastObject,這樣就可以找到FirstViewController了,然後跳轉的時候就和正常的跳轉一樣了
JTNavigationController *nav=(JTNavigationController *)self.tabController.viewControllers[0];
UIViewController *vc=(UIViewController*)nav.jt_viewControllers.lastObject;
[vc.navigationController pushViewController:VC animated:NO];
}else {
}
- (void)readNotificationVcWithUserInfo:(NSDictionary *)userInfo {
MessageViewController *vc = [[MessageViewController alloc] init];
challengeVc.userInfo = userInfo;
BaseNavigationController *challengeNaviVc = [[BaseNavigationController alloc] initWithRootViewController:challengeVc];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}