ios 退出程序通知后台_IM-iOS退出后台接受消息,app退出后台能接收到推送

本文介绍了当iOS App处于后台或被关闭时如何利用本地推送 UILocalNotification 实现消息通知。通过在applicationDidEnterBackground方法中注册通知,在messageCome:方法中处理通知内容。同时,为防止重复通知,在applicationWillEnterForeground方法中移除通知。
摘要由CSDN通过智能技术生成

App被失活状态的时候可以走苹果的APNS;但是在活跃的时候却接受不到推送!

那就用到本地推送:UILocalNotification 消息神器。

处理不好可能会有很多本地推送到来,那么问题来了要在什么地方去注册通知?什么地方去移除通知?

一、要在什么地方去注册通知

- (void)applicationDidEnterBackground:(UIApplication *)application;

手机刚进入后台会走的方法,applicationDidEnterBackground;

我会注册一个通知:名字宏定义

/**应用获取到刷新推送消息提醒*/

#define kString_NSNotificationCenterRefreshMessageData    @"kString_NSNotificationCenterRefreshMessageData"

在AppDelegate.m的 applicationDidEnterBackground方法里边添加通知

- (void)applicationDidEnterBackground:(UIApplication *)application{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCome:) name:kString_NSNotificationCenterRefreshMessageData object:nil];

}

- (void)messageCome:(NSNotification *)notifi{

if (![notifi.name isEqualToString:kString_NSNotificationCenterRefreshMessageData]) {

return;

}

dispatch_async(dispatch_get_main_queue(), ^{

[self notifi:notifi];

});

}

- (void)notifi:(NSNotification *)notifi{

NSMutableString * notifiMessage = nil;

RCMessage *message = notifi.object;

if (message.conversationType == ConversationType_SYSTEM) {

notifiMessage = [[NSMutableString alloc]initWithString: @"猎上网:"];

}else if(message.conversationType == ConversationType_PRIVATE){

MessageUser *user =  [[MyFMDB sharedMyFMDB] findUserWithID:[message.senderUserId intValue]];

if (user.name&&![user.name isEqualToString:@""]) {

notifiMessage = [[NSMutableString alloc]initWithString: [NSString stringWithFormat:@"%@:",user.name]];

}

}else{

return;

}

NSMutableDictionary * inforDic = [NSMutableDictionary dictionary];

UILocalNotification * locNoti = [[UILocalNotification alloc]init];

if ([message.content isKindOfClass:[RCTextMessage class]]) {

RCTextMessage *textMessage = (RCTextMessage *)message.content;

[notifiMessage appendString:textMessage.content];

[inforDic setValue:textMessage.content forKey:@"name"];

}else if([message.content isKindOfClass:[RCImageMessage class]]){

[notifiMessage appendString:@"图片"];

[inforDic setValue:@"图片" forKey:@"name"];

}else if([message.content isKindOfClass:[RCVoiceMessage class]]){

[notifiMessage appendString:@"语音"];

[inforDic setValue:@"语音" forKey:@"name"];

}else if([message.content isKindOfClass:[IMPositionMessage class]]){

[notifiMessage appendString:@"职位名片"];

[inforDic setValue:@"职位名片" forKey:@"name"];

}else if([message.content isKindOfClass:[IMSwapPhoneMessage class]]){

[notifiMessage appendString:@"交换电话"];

[inforDic setValue:@"交换电话" forKey:@"name"];

}else if([message.content isKindOfClass:[IMResumeMessage class]]){

[notifiMessage appendString:@"简历名片"];

[inforDic setValue:@"简历名片" forKey:@"name"];

}else if([message.content isKindOfClass:[TaskedPositionToHunteron class]]){

TaskedPositionToHunteron *textMessage = (TaskedPositionToHunteron *)message.content;

[notifiMessage appendString:[NSString stringWithFormat:@"PA(%@)为您定向推荐了一个新的职位( #%lld %@)。",textMessage.paName,textMessage.positionId,textMessage.positionName]];

[inforDic setValue:textMessage.paName forKey:@"paName"];

[inforDic setValue:[NSString stringWithFormat:@"%lld",textMessage.positionId]  forKey:@"positionId"];

[inforDic setValue:textMessage.positionName forKey:@"positionName"];

}

//1.1 设置通知的内容

locNoti.alertAction = notifiMessage; // 锁屏状态下显示: 滑动来快点啊

locNoti.alertBody = notifiMessage;

//1.2 设置通知的发送时间

locNoti.fireDate = [NSDate date];

locNoti.userInfo =inforDic;

//1.3 设置时区,一般默认

locNoti.timeZone = [NSTimeZone defaultTimeZone];

// 设置通知发送时, 提醒数字(==0, 会自动消失)

locNoti.applicationIconBadgeNumber = 0;

locNoti.repeatInterval = 0;

// 2. 发送通知

[[UIApplication sharedApplication]scheduleLocalNotification:locNoti];

NSLog(@"====%d",[NSThread isMainThread]);

[[UIApplication sharedApplication]cancelLocalNotification:locNoti];

}

二、什么地方去移除通知

手机刚进入前台会走的方法

- (void)applicationWillEnterForeground:(UIApplication *)application{

[[NSNotificationCenter defaultCenter] removeObserver:self name:kString_NSNotificationCenterRefreshMessageData object:nil];

}

因为手机不活跃的时候不能立即发通知!记住是立即,又不是延迟发本地推送,所以不需要处理已经不活跃的情况!要在进入前台的时候移除通知,要不然下次在进入后台会在此注册通知!就会显示两条本地推送!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值