iOS 遠程推送 根據馬甲包後台推送內容的不同跳轉指定頁面

iOS 遠程推送 根據馬甲包後台推送內容的不同跳轉指定頁面

來自專欄 ios2.1大禮包被拒經驗分享

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秒左右跳轉新頁面(我做推送的時候直接 跳轉會出現異常異常),這種情況不需要判斷當前的 controller2、從第二種方法裡邊獲取信息, 判斷當前controller,是否需要push新的頁面,或者回到tabbar

下面是 我這邊的代碼,具體 信息 欄位根據你們後台來定

使用到了 類別,方便統一管理

推送平台根據你們自己所使用的來配置 ()

[objc] view plain copy

[objc] view plain copy// 接收 遠程推送消息後 彈出 新的頁面 // 應用未啟動的 情況下點擊推送消息 進入應用 觸發didFinishLaunchingWithOptions 此方法, 並從字典中 獲取到推送的相關消息 // 應用已經啟動 處於 活躍狀態 或 非活躍狀態 會觸發didReceiveRemoteNotification 此方法, 並從字典中 獲取到推送相關消息

#import "AppDelegate+UMessage.h" #import "AppDelegate+UMSocal.h" #import "ArticleOrProjectDetailViewController.h" #define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define _IPHONE80_ 80000 @implementation AppDelegate (UMessage) - (void)setUmessageWithlaunchOptions:(NSDictionary *)launchOptions { [UMessage setLogEnabled:YES]; //友盟推送 key [UMessage startWithAppkey:@"x x x x x x" launchOptions:launchOptions]; //註冊推送功能 [self registerRemoteMessage]; [self didReceiveRemoteJingWhenAppDeadWithDic:launchOptions]; } //註冊遠程推送 - (void)registerRemoteMessage { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { //register remoteNotification types UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action1_identifier"; action1.title=@"Accept"; action1.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程序 UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕 action2.identifier = @"action2_identifier"; action2.title=@"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程序,在後台處理 action2.authenticationRequired = YES; //需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; action2.destructive = YES; UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @"category1";//這組動作的唯一標示 [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)]; UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:categorys]]; [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else{ //register remoteNotification types [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; } #else [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; #endif } - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo { //關閉友盟自帶的彈出框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; NSDictionary *dic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:userInfo[@"msg"] key:DMDESKEYS]] ; if ([[dic objectForKey:@"type"] isEqualToString:@"articles"] || [[dic objectForKey:@"type"] isEqualToString:@"projects"]) { [self didReceiveRemoteJingWhenAppBackgroundWithDic:dic]; } else { [self didReceiveRemoteMessageWhenAppBackgroundWithDic:userInfo]; } } #pragma mark ----應用掛起的狀態 時接收到每日文章項目推送 - (void)didReceiveRemoteJingWhenAppBackgroundWithDic:(NSDictionary *)dic { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ UITabBarController *tab = (UITabBarController *)self.window.rootViewController; UINavigationController *nvc = tab.selectedViewController; UIViewController *vc = nvc.visibleViewController; if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { //防止同一界面多次 push if ([vc isMemberOfClass:[ArticleOrProjectDetailViewController class]]) { ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)vc; if ([dic[@"type"] isEqualToString:@"articles"]) { avc.detailDataTpe = DetailDataTypeArticle; } else { avc.detailDataTpe = DetailDataTypeProject; } avc.titleString = dic[@"title"]; avc.idString = dic[@"conId"]; avc.iconURLString = dic[@"cover"]; [avc reloadAll]; } else { ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:dic]; [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc]; } } }); } #pragma mark ----- 應用掛起的狀態,收到 私信 關注等 - (void)didReceiveRemoteMessageWhenAppBackgroundWithDic:(NSDictionary *)userInfo { NSDictionary *subDic = userInfo[@"aps"]; NSString *str = [NSString stringWithFormat:@"%@",userInfo]; [self.window.rootViewController.view makeToast:subDic[@"alert"]]; if (![str isEqualToString:@"(null)"]) { // 後台返回數據轉為字元串後為(null)用此來判斷 [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatMessage" object:nil]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:@"myNewRemindMessage" object:[NSString stringWithFormat:@"%@",subDic[@"badge"]]]; } [[NSNotificationCenter defaultCenter] postNotificationName:@"didreceiveNoti" object:userInfo]; [[NSNotificationCenter defaultCenter] postNotificationName:@"messagelistrefresh" object:nil]; if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; UINavigationController *nvc = tbc.selectedViewController; UIViewController *vc = nvc.visibleViewController; [vc.navigationController popToRootViewControllerAnimated:YES]; tbc.tabBarController.tabBar.hidden = NO; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ tbc.selectedIndex = 2; }); } } #pragma mark ------應用從關閉的 狀態下啟動時 - (void)didReceiveRemoteJingWhenAppDeadWithDic:(NSDictionary *)sic { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ if ([sic.allKeys containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) { NSDictionary *subDic = [sic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; subDic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:[subDic objectForKey:@"msg"] key:DMDESKEYS]] ; ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:subDic]; UITabBarController *tbc = (UITabBarController *)self.window.rootViewController; if ([[subDic objectForKey:@"type"] isEqualToString:@"articles"] || [[subDic objectForKey:@"type"] isEqualToString:@"projects"]) { UINavigationController *nc = tbc.selectedViewController; UIViewController *vc = nc.visibleViewController; [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc]; } else { tbc.selectedIndex = 2; } } }); } #pragma mark ------推送詳情頁 - (UIViewController *)getWebDetailWithData:(NSDictionary *)dic{ ArticleOrProjectDetailViewController *avc = [[ArticleOrProjectDetailViewController alloc] init]; if ([dic[@"type"] isEqualToString:@"articles"]) { avc.detailDataTpe = DetailDataTypeArticle; } else { avc.detailDataTpe = DetailDataTypeProject; } avc.idString = dic[@"conId"]; avc.iconURLString = dic[@"cover"]; return avc; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { [self didReceiveRemoteNotification:userInfo]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [UMessage registerDeviceToken:deviceToken]; } + (void)addAlias { [UMessage addAlias:"xxxxxx" type:@"xxxxx" response:^(id responseObject, NSError *error) { }]; } //註銷 個人推送 註冊一個空的 alias。 用於移除個人推送, 同時 文章、項目推送不影響 + (void)removeAlias { [UMessage addAlias:[@"" MD5Digest] type:@"xxxxx" response:^(id responseObject, NSError *error) { }]; }

推薦閱讀:

黑客蒸米:一個大V的生活意見
網站設計技術與美工的配合 - 知識庫 - 綜合知識管理解決方案|紹林科技|軟體就是思想
法格破壁機BV-2711FWE使用測評
郯城縣楊集鎮千餘「科技大嫂」增收忙
國內伺服器,價格受哪些因素影響?

TAG:科技 | 計算機視覺 | iOS |