【iOS】注册推送及处理推送,打开界面
2016-08-24 00:00
232 查看
摘要: AppDelegate
registerForRemoteNotifications
UITabBarController *tabBarController = (UITabBarController*)self.window.rootViewController;
UINavigationController * navgation = (UINavigationController *)tabBarController.selectedViewController;
推送注册及处理推送,应用在前台时弹出 alert,应用在后台或通过推送打开应用时,直接进入推送页面。
注册通知 ios8
接受通知,解析参数
处理push页面
应用未运行,launchOptions判断,保存推送数据 ,启动界面后再打开推送页面
registerForRemoteNotifications
UITabBarController *tabBarController = (UITabBarController*)self.window.rootViewController;
UINavigationController * navgation = (UINavigationController *)tabBarController.selectedViewController;
推送注册及处理推送,应用在前台时弹出 alert,应用在后台或通过推送打开应用时,直接进入推送页面。
注册通知 ios8
接受通知,解析参数
处理push页面
应用未运行,launchOptions判断,保存推送数据 ,启动界面后再打开推送页面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /************ 注册通知 **************/ if (launchOptions) { NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) { //接收到远程推送进行相应的逻辑处理,先保存数据,启动界面后再处理推送页面 NSLog(@"launchOptions: %@", userInfo); [Utility saveAllValueWithKey:@"launchOptions" withValue:userInfo]; } } if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; //Actions UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; acceptAction.identifier = @"ACCEPT_IDENTIFIER"; acceptAction.title = @"Accept"; acceptAction.activationMode = UIUserNotificationActivationModeForeground; acceptAction.destructive = NO; acceptAction.authenticationRequired = NO; //Categories UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; inviteCategory.identifier = @"INVITE_CATEGORY"; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal]; NSSet *categories = [NSSet setWithObjects:inviteCategory, nil]; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { // [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } return YES; } #pragma mark - XGPush ////本地通知,应用在前台,不用弹出alert,单独处理 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ //notification是发送推送时传入的字典信息 [XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"]; //删除推送列表中的这一条 [XGPush delLocalNotification:notification]; //[XGPush delLocalNotification:@"clockID" userInfoValue:@"myid"]; //清空推送列表 //[XGPush clearLocalNotifications]; } #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ //注册UserNotification成功的回调 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //用户已经允许接收以下类型的推送 //UIUserNotificationType allowedTypes = [notificationSettings types]; NSLog(@"__notificationSettings__ %@", notificationSettings); } //按钮点击事件回调 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){ NSLog(@"ACCEPT_IDENTIFIER is clicked"); } NSLog(@"%@:%@", userInfo, identifier); completionHandler(); } #endif #pragma mark 接受注册存储token - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults removeObjectForKey:@"deviceToken"]; [userDefaults setObject:deviceToken forKey:@"deviceToken"]; NSLog(@"My token is: %@", [userDefaults objectForKey:@"deviceToken"]); void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush Demo]register successBlock"); }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush Demo]register errorBlock"); }; #ifdef DEBUG // 设置账号 [XGPush setAccount:@"12345678"]; #endif //注册设备 NSString * deviceTokenStr = [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock]; //如果不需要回调 // [XGPush registerDevice:deviceToken]; //打印获取的deviceToken的字符串 NSLog(@"[XGPush Demo] deviceTokenStr is %@",deviceTokenStr); } //如果deviceToken获取不到会进入此事件 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@",err]; NSLog(@"[XGPush Demo]%@",str); } - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { NSLog(@"%@", userInfo); //回调版本示例 void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush]handleReceiveNotification successBlock"); //#ifdef DEBUG // [application setApplicationIconBadgeNumber:(application.applicationIconBadgeNumber-1)]; NSInteger state = [UIApplication sharedApplication].applicationState; if (state == UIApplicationStateActive ) { //应用在前台 NSDictionary *apsDictionary = [userInfo objectForKey:@"aps"]; NSString * content = [apsDictionary objectForKey:@"alert"]; [self alertPush:content withUserInfo:userInfo]; } else if( state == UIApplicationStateInactive){ //应用从后台->前台 [self pushNext:userInfo]; } //state == UIApplicationStateBackground //应用在后台 //#endif }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush]handleReceiveNotification errorBlock"); }; void (^completion)(void) = ^(void){ //完成之后的处理 NSLog(@"[xg push completion]userInfo is %@",userInfo); }; //推送反馈(app运行时) // [XGPush handleReceiveNotification:userInfo]; [XGPush handleReceiveNotification:userInfo successCallback:successBlock errorCallback:errorBlock completion:completion]; } #pragma mark - Push - (void)pushNext:(NSDictionary*)userInfo { UITabBarController *tabBarController = (UITabBarController*)self.window.rootViewController; UINavigationController * navgation = (UINavigationController *)tabBarController.selectedViewController; NSString * urlType = userInfo[@"URL"]; if(urlType.length) { NSString * url = [userInfo objectForKey:@"URL"]; url = [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURL:[NSURL URLWithString:url]]; webViewController.hidesBottomBarWhenPushed = YES; [navgation pushViewController:webViewController animated:YES]; }else{ } } #pragma mark - Alert - (void)alertPush:(NSString*)mes withUserInfo:(NSDictionary*)userInfo { UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:mes preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self pushNext:userInfo]; }]; [alert addAction:cancelAction]; [alert addAction:okAction]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; }
相关文章推荐
- iOS根视图切换,释放内存,以及推送界面跳转(iOS多语言切换语言处理,也请看此处)
- IOS 处理消息推送,并跳转到相关界面
- 如何避免打开键盘或横竖屏切换,active重启,处理界面的延迟动画
- iOS应用内打开App Store应用详情界面
- IOS高访新浪微博界面(讲解如何自定义UITableViewCell,处理@#链接 特殊字符)
- iOS—应用内打开App Store应用详情界面
- iOS—接收到APNs远程推送消息时的处理流程
- 如何避免打开键盘或横竖屏切换,active重启,处理界面的延迟动画
- 用IOS做一个界面切换的效果(登录界面和注册界面和找回密码界面的切换)(用封装好的lable和textf创建界面)
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- IOS中判断软件消息推送是否打开
- ios 注册打开方式
- 控件打开设计器界面报错处理
- iOS处理通话或录音状态中界面错乱的重布局
- iOS处理通话或录音状态中界面错乱的重布局
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 如何避免打开键盘或横竖屏切换,active重启,处理界面的延迟动画
- 界面代码ios不响应presentModalViewController界面的处理
- IOS聊天界面键盘处理
- ios 打开图片库和相机选择图片界面修改为简体中文