iOS本地通知
今天项目中用到了消息推送第一次做这方方面的内容先记录下来。
这个项目采用的时第三方的极光推送按照极光的文档配置好后,
当有推送时会在以下方法中收到服务器的推送通知,以及推送消息内容。
- (void)networkDidReceiveMessage:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSDictionary *extras = userInfo[@"extras"]; NSString *type = extras[@"type"]; //NSString *content = [userInfo valueForKey:@"content"]; [self handleRemoteNotification:userInfo];//获得推送内容,调用处理推送的方法 }
处理推送要判断App当前的状态
- (void)handleRemoteNotification:(NSDictionary *)userInfo{ if(userInfo[@"_j_msgid"] || userInfo[@"content"]){ UIApplicationState state=[UIApplication sharedApplication].applicationState; if(state==UIApplicationStateActive || state==UIApplicationStateInactive){//在前台 [[NSNotificationCenter defaultCenter]postNotificationName:r7_fetchRemoteNotificationNotify object:nil userInfo:userInfo]; SuperNavigationController *navCtrl = (SuperNavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController; SuperTabController *tabCtrl=(SuperTabController*)navCtrl.rootViewController; [tabCtrl dealWithPushInfo:userInfo applicationState:state]; if(state==UIApplicationStateInactive){ [APService setBadge:0]; [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0]; } } else if(state==UIApplicationStateBackground){//通知推送至后台 _userInfo=userInfo; [self addLocationRemind]; } [APService handleRemoteNotification:userInfo]; DLog(@"userInfo:%@",userInfo); } }
- (void)addLocationRemind { GlobalSingle *single = [GlobalSingle sharedSingleInstace]; NSInteger discussDynamicNumber = [single numberToIntWithKey:DYNAMICNUMBERKEY] + 1; [single saveNumberWith:discussDynamicNumber key:DYNAMICNUMBERKEY]; NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5]; UILocalNotification *note = [[UILocalNotification alloc] init]; note.alertBody = @"来自NEUQer的提醒"; note.alertAction = @"回复"; // note.alertLaunchImage = @"discuss_selected"; note.applicationIconBadgeNumber = 1; note.soundName = UILocalNotificationDefaultSoundName; note.fireDate = date; [[UIApplication sharedApplication] scheduleLocalNotification:note]; }