1.注册推送
- (BOOL)pushNotificationOpen { if (isAfterIOS8) { UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types; return (types & UIRemoteNotificationTypeAlert); } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; return (types & UIRemoteNotificationTypeAlert); } }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self initvalues]; if (isAfterIOS8) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; } if (![self pushNotificationOpen]) { self.localDeviceToken=@"ios_devicetoken_disabled"; } NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if(userInfo) { self.launchFromNotigication=YES; [self createPushViewIndexWithDic:userInfo]; } }
-(void)createPushViewIndexWithDic:(NSDictionary *)userInfo { NSString*str=[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; NSString*rent=@"发财圈"; NSString*second=@"房源"; NSString*chatMsg=@"消息"; NSRange range1 = [[str uppercaseString] rangeOfString:[rent uppercaseString]]; if (range1.location!=NSNotFound) { self.pushViewIndex=3; } NSRange range2 = [[str uppercaseString] rangeOfString:[second uppercaseString]]; if (range2.location!=NSNotFound) { self.pushViewIndex=3; } NSRange range3 = [[str uppercaseString] rangeOfString:chatMsg]; if (range3.location!=NSNotFound) { self.pushViewIndex=2; } }
2.获得消息
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString* tokeStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; NSMutableString *mstr = [NSMutableString stringWithString:tokeStr]; NSRange range = [mstr rangeOfString:@" "]; while (range.location != NSNotFound) { [mstr deleteCharactersInRange:range]; range = [mstr rangeOfString:@" "]; } tokeStr=[NSString stringWithString:mstr]; if ([tokeStr isEqualToString:@""] || !tokeStr) { tokeStr=@"ios_devicetoken_disabled"; } self.localDeviceToken=tokeStr; } -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"注册失败"); } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [HttpRequest httpRequestGetNewStsate]; if (self.tabbarView!=nil) { if (self.becomeForground==NO||self.appActive==NO||self.launchFromNotigication==YES) { self.becomeForground=YES; NSString*str=[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; NSString*snsStr=@"发财圈"; NSString*rent=@"个人租房"; NSString*second=@"个人二手房"; NSString*chatMsg=@"消息"; NSRange range1 = [[str uppercaseString] rangeOfString:[rent uppercaseString]]; if (range1.location!=NSNotFound) { self.rentRed=YES; self.getPush=YES; self.tabbarView.selectedIndex=3; [self.tabbarView.foundNaviVC popToRootViewControllerAnimated:YES]; } NSRange range2 = [[str uppercaseString] rangeOfString:[second uppercaseString]]; if (range2.location!=NSNotFound) { self.secondRed=YES; self.getPush=YES; self.tabbarView.selectedIndex=3; [self.tabbarView.foundNaviVC popToRootViewControllerAnimated:YES]; } NSRange range3 = [[str uppercaseString] rangeOfString:chatMsg]; if (range3.location!=NSNotFound) { self.mustRefreshFriendList=YES; self.getPush=YES; self.tabbarView.selectedIndex=2; [self.tabbarView.messageNaviVC popToRootViewControllerAnimated:YES]; } NSRange range4 = [[str uppercaseString] rangeOfString:snsStr]; if (range4.location!=NSNotFound) { self.getPush=YES; self.tabbarView.selectedIndex=3; [self.tabbarView.foundNaviVC popToRootViewControllerAnimated:YES]; } }else { [ProgressHUD showSuccess:@"收到新的消息"]; } } }