项目之前使用的信鸽推送,觉得不怎么好用,就在闲下来的时候换成了个推,个推的文档还是比较详细的,简单的推送只要看下文档就OK了。
透传
个推有个透传的功能,使用这个功能进行推送的时候,如果APP在前台运行时,是不会显示推送信息的;如果APP是在后台或者杀死的情况下,是会有推送通知的显示的。这个一定要和推送通知区分开来。(最重要的是要和后台人员商量一致,再传参数的时候是使用透传还是消息通知,如果使用透传功能进行参数的传递,那么需要在透传的回调方法中自己写本地通知)
消息通知
主要使用就是带参数的消息通知
app在离线状态接收到消息通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)( ))completionHandler {
//获取消息中传递的参数
NSDictionary * dict = [[NSDictionary alloc]initWithDictionary:response.notification.request.content.userInfo];
}
苹果为我们提供的app在前台接收到消息通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
//获取消息中传递的参数
NSDictionary * dict = [[NSDictionary alloc]initWithDictionary:notification.request.content.userInfo];
}
但是在使用个推的时候,我们的app与个推的服务器保持了长连接,我们在透传的时候使用下面的方法来接收参数:
/** SDK收到透传消息回调*/
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *) taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString * )appId;
根据接收到的参数跳转到不同的页面:
-(void)touChuanPushAction:(NSDictionary *)dic {
int type = [[dic objectForKey:@"type"] intValue];
switch (type) {
case 10:{//跳转消息详情
}break;
case 11:{//跳转web页或者类包名
if ([[dic objectForKey:@"isweb"] intValue] == 1) {//isweb 1web页面地址 2类包名
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"进入web" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"OK", nil];//离线
[alert show];
NSString *urlString = [dic objectForKey:@"weburl"];
WebUrlViewController *VC=[[WebUrlViewController alloc]init];
if (urlString == nil || [urlString isEqualToString:@""]) {
urlString = @"www.gexiazi.com";
}
VC.url= urlString;
VC.Name=@"歌匣子活动";
[self PushToViewControllerWithVC:VC];
}else if ([[dic objectForKey:@"isweb"] intValue] == 2) {
NSString *className = [dic objectForKey:@"iosClassName"];
[self pushToViewControllerWithClassName:className];
}
}break;
case 12:{//ktv商家审核通过
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"进入" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"OK", nil];
[alert show];
UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
UINavigationController *nvc = tab.selectedViewController;
UIViewController *vc = nvc.visibleViewController;
KTVShopCenterViewController *VC = [[KTVShopCenterViewController alloc]init];
vc.hidesBottomBarWhenPushed=YES;
[vc.navigationController pushViewController:VC animated:YES];
vc.hidesBottomBarWhenPushed=NO;
}break;
case 13:{//ktv商家审核拒绝
}break;
case 14:{//ktv拼团订单支付成功推送商家
KTVShopCenterViewController *VC = [[KTVShopCenterViewController alloc]init];
[self PushToViewControllerWithVC:VC];
}break;
case 15:{//ktv拼团订单支付成功推送用户
WaitForUseOrderInfoViewController *VC = [[WaitForUseOrderInfoViewController alloc]init];
[self PushToViewControllerWithVC:VC];
}break;
case 16:{//ktv商家线上游戏上架
}break;
case 17:{//ktv商家拼团套餐成功或失败推送商家
}break;
case 18:{//ktv商家拼团套餐成功或失败推送用户
PinTuanZhuangTaiViewController *VC = [[PinTuanZhuangTaiViewController alloc] init];
[self PushToViewControllerWithVC:VC];
}break;
default:
break;
}
}
//根据类名来跳转对应界面
-(void)pushToViewControllerWithClassName:(NSString *)className {
if (className != nil) {
id myObj = [[NSClassFromString(className) alloc] init];
@try {
UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
UINavigationController *nvc = tab.selectedViewController;
UIViewController *vc = nvc.visibleViewController;
vc.hidesBottomBarWhenPushed = YES;
[vc.navigationController pushViewController:myObj animated:YES];
vc.hidesBottomBarWhenPushed = NO;
}
@catch (NSException *exception) {
// 捕获到的异常exception
}
@finally {
// 结果处理
}
}
}
-(void)PushToViewControllerWithVC:(UIViewController *)vc {
UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
UINavigationController *nvc = tab.selectedViewController;
UIViewController *viewController = nvc.visibleViewController;
viewController.hidesBottomBarWhenPushed=YES;
[viewController.navigationController pushViewController:vc animated:YES];
viewController.hidesBottomBarWhenPushed=NO;
}