轉發自:http://www.jianshu.com/p/4531bd6e3a01
iOS 遠程推送,根據后台推送內容的不同, 跳轉指定頁面 我目前的需求是總體分為兩類: 1:私信、關注、點贊一類,只需跳轉到對應的tabbar 中的某一項 2:每日精品文章項目推送,分兩個子類 (1)如果當前已經打開 文章項目頁面,則直接刷新,不推出新頁面 (2)如果當前未打開此頁面,則push出新的文章項目頁面 iOS 推送情況分為 應用未啟動的 情況: 打開應用 ,推送信息 會通過 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions: 此方法傳遞,內容在launchOptions里面, 通過控制台即可看出 應用已啟動的 情況:(1)應用在前台活躍 (前台活躍 只做 頁面的體型,alert,不去主動幫用 戶打開新的界面) (2)應用在后台掛起 (后台掛起轉為 活躍時 調用此方法) - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo 結論,我們要做的就是 通過系統的兩個方法拿到推送信息后: 1、 如果是從第一個方法里邊獲得的信息, 則 延遲0.5秒左右跳轉新頁面(我做推送的時候直接 跳轉會出現異常異常),這種情況不需要判斷當前的 controller 2、從第二種方法里邊獲取信息, 判斷當前controller,是否需要push新的頁面,或者回到tabbar 下面是 我這邊的代碼,具體 信息 字段根據你們后台來定 使用到了 類別,方便統一管理 推送平台根據你們自己所使用的來配置 () [objc] view plain copy [objc] view plain copy // 接收 遠程推送消息后 彈出 新的頁面 // 應用未啟動的 情況下點擊推送消息 進入應用 觸發didFinishLaunchingWithOptions 此方法, 並從字典中 獲取到推送的相關消息 // 應用已經啟動 處於 活躍狀態 或 非活躍狀態 會觸發didReceiveRemoteNotification 此方法, 並從字典中 獲取到推送相關消息
1 #import "AppDelegate+UMessage.h" 2 #import "AppDelegate+UMSocal.h" 3 #import "ArticleOrProjectDetailViewController.h" 4 5 #define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 6 7 #define _IPHONE80_ 80000 8 9 @implementation AppDelegate (UMessage) 10 11 - (void)setUmessageWithlaunchOptions:(NSDictionary *)launchOptions { 12 [UMessage setLogEnabled:YES]; 13 //友盟推送 key 14 [UMessage startWithAppkey:@"x x x x x x" launchOptions:launchOptions]; 15 //注冊推送功能 16 [self registerRemoteMessage]; 17 [self didReceiveRemoteJingWhenAppDeadWithDic:launchOptions]; 18 } 19 20 //注冊遠程推送 21 - (void)registerRemoteMessage { 22 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ 23 if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) 24 { 25 //register remoteNotification types 26 UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init]; 27 action1.identifier = @"action1_identifier"; 28 action1.title=@"Accept"; 29 action1.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程序 30 31 UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕 32 action2.identifier = @"action2_identifier"; 33 action2.title=@"Reject"; 34 action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程序,在后台處理 35 action2.authenticationRequired = YES; 36 //需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; 37 action2.destructive = YES; 38 39 UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; 40 categorys.identifier = @"category1";//這組動作的唯一標示 41 [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)]; 42 43 UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert 44 categories:[NSSet setWithObject:categorys]]; 45 [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings]; 46 47 [[UIApplication sharedApplication] registerForRemoteNotifications]; 48 49 } else{ 50 //register remoteNotification types 51 [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge 52 |UIRemoteNotificationTypeSound 53 |UIRemoteNotificationTypeAlert]; 54 } 55 #else 56 [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge 57 |UIRemoteNotificationTypeSound 58 |UIRemoteNotificationTypeAlert]; 59 60 #endif 61 62 } 63 64 - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo { 65 //關閉友盟自帶的彈出框 66 [UMessage setAutoAlert:NO]; 67 [UMessage didReceiveRemoteNotification:userInfo]; 68 69 NSDictionary *dic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:userInfo[@"msg"] key:DMDESKEYS]] ; 70 71 if ([[dic objectForKey:@"type"] isEqualToString:@"articles"] || 72 [[dic objectForKey:@"type"] isEqualToString:@"projects"]) { 73 [self didReceiveRemoteJingWhenAppBackgroundWithDic:dic]; 74 } else { 75 [self didReceiveRemoteMessageWhenAppBackgroundWithDic:userInfo]; 76 } 77 } 78 79 80 #pragma mark ----應用掛起的狀態 時接收到每日文章項目推送 81 - (void)didReceiveRemoteJingWhenAppBackgroundWithDic:(NSDictionary *)dic { 82 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); 83 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 84 UITabBarController *tab = (UITabBarController *)self.window.rootViewController; 85 UINavigationController *nvc = tab.selectedViewController; 86 UIViewController *vc = nvc.visibleViewController; 87 if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { 88 //防止同一界面多次 push 89 if ([vc isMemberOfClass:[ArticleOrProjectDetailViewController class]]) { 90 ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)vc; 91 if ([dic[@"type"] isEqualToString:@"articles"]) { 92 avc.detailDataTpe = DetailDataTypeArticle; 93 } else { 94 avc.detailDataTpe = DetailDataTypeProject; 95 } 96 avc.titleString = dic[@"title"]; 97 avc.idString = dic[@"conId"]; 98 avc.iconURLString = dic[@"cover"]; 99 [avc reloadAll]; 100 } else { 101 ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:dic]; 102 [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc]; 103 } 104 } 105 }); 106 } 107 108 #pragma mark ----- 應用掛起的狀態,收到 私信 關注等 109 - (void)didReceiveRemoteMessageWhenAppBackgroundWithDic:(NSDictionary *)userInfo { 110 NSDictionary *subDic = userInfo[@"aps"]; 111 NSString *str = [NSString stringWithFormat:@"%@",userInfo]; 112 [self.window.rootViewController.view makeToast:subDic[@"alert"]]; 113 if (![str isEqualToString:@"(null)"]) { // 后台返回數據轉為字符串后為(null)用此來判斷 114 [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatMessage" object:nil]; 115 } else { 116 [[NSNotificationCenter defaultCenter] postNotificationName:@"myNewRemindMessage" object:[NSString stringWithFormat:@"%@",subDic[@"badge"]]]; 117 } 118 [[NSNotificationCenter defaultCenter] postNotificationName:@"didreceiveNoti" object:userInfo]; 119 [[NSNotificationCenter defaultCenter] postNotificationName:@"messagelistrefresh" object:nil]; 120 121 if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { 122 UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; 123 UINavigationController *nvc = tbc.selectedViewController; 124 UIViewController *vc = nvc.visibleViewController; 125 [vc.navigationController popToRootViewControllerAnimated:YES]; 126 tbc.tabBarController.tabBar.hidden = NO; 127 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); 128 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 129 tbc.selectedIndex = 2; 130 }); 131 } 132 } 133 134 #pragma mark ------應用從關閉的 狀態下啟動時 135 - (void)didReceiveRemoteJingWhenAppDeadWithDic:(NSDictionary *)sic { 136 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); 137 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 138 if ([sic.allKeys containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) { 139 NSDictionary *subDic = [sic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 140 subDic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:[subDic objectForKey:@"msg"] key:DMDESKEYS]] ; 141 ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:subDic]; 142 UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; 143 if ([[subDic objectForKey:@"type"] isEqualToString:@"articles"] || 144 [[subDic objectForKey:@"type"] isEqualToString:@"projects"]) { 145 UINavigationController *nc = tbc.selectedViewController; 146 UIViewController *vc = nc.visibleViewController; 147 [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc]; 148 } else { 149 tbc.selectedIndex = 2; 150 } 151 } 152 }); 153 } 154 155 156 157 #pragma mark ------推送詳情頁 158 - (UIViewController *)getWebDetailWithData:(NSDictionary *)dic{ 159 ArticleOrProjectDetailViewController *avc = [[ArticleOrProjectDetailViewController alloc] init]; 160 if ([dic[@"type"] isEqualToString:@"articles"]) { 161 avc.detailDataTpe = DetailDataTypeArticle; 162 } else { 163 avc.detailDataTpe = DetailDataTypeProject; 164 } 165 avc.idString = dic[@"conId"]; 166 avc.iconURLString = dic[@"cover"]; 167 return avc; 168 } 169 170 171 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { 172 [self didReceiveRemoteNotification:userInfo]; 173 } 174 175 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 176 { 177 [UMessage registerDeviceToken:deviceToken]; 178 } 179 180 + (void)addAlias { 181 182 [UMessage addAlias:"xxxxxx" type:@"xxxxx" response:^(id responseObject, NSError *error) { 183 }]; 184 } 185 186 //注銷 個人推送 注冊一個空的 alias。 用於移除個人推送, 同時 文章、項目推送不影響 187 + (void)removeAlias { 188 [UMessage addAlias:[@"" MD5Digest] type:@"xxxxx" response:^(id responseObject, NSError *error) { 189 }]; 190 }