转发自: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
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 }