您的位置:首页 > 移动开发 > IOS开发

【iOS】注册推送及处理推送,打开界面

2016-08-24 00:00 232 查看
摘要: AppDelegate
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 推送