IM-iOS退出后台接受消息,app退出后台能接收到推送

简介: App被失活状态的时候可以走苹果的APNS;但是在活跃的时候却接受不到推送!那就用到本地推送:UILocalNotification 消息神器。处理不好可能会有很多本地推送到来,那么问题来了要在什么地方去注册通知?什么地方去移除通知?一、要在什么地方去注册通知- (void)applicat...

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];

 

}

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

 

相关文章
|
2月前
|
架构师 Java
jvm性能调优实战 - 35电商APP后台系统如何对Full GC进行深度优化
jvm性能调优实战 - 35电商APP后台系统如何对Full GC进行深度优化
67 0
|
9天前
|
存储 安全 前端开发
APP管理后台OSS技术改造
旨在记录之前使用的上传文件是放在服务器的现在改成了oss更加高效管理
|
2月前
|
Android开发 iOS开发
ios后台播放声音的三种实现方式
ios后台播放声音的三种实现方式
47 1
|
25天前
|
存储 Android开发 Kotlin
Kotlin开发安卓app,在使用 MediaPlayer 播放 res/raw 中的音乐时遇到突然中断的问题,而 onErrorListener 没有接收到任何报错
在使用 Android MediaPlayer 播放 res/raw 中的音乐时遇到中断问题,可能的原因包括资源问题、媒体文件编码格式、生命周期管理和设备资源配置。要排查问题,检查音频文件是否正确包含,格式编码是否支持,MediaPlayer 是否正确管理及释放,以及设备是否有足够存储和配置。通过设置 onErrorListener 日志和确保在 onDestroy 中释放资源来调试。如果文件过大,考虑使用 AssetManager。遵循这些步骤可帮助诊断并解决播放中断的问题。
|
2月前
|
iOS开发
iOS中如何显示后台返回的带有html标签的富文本字符串
iOS中如何显示后台返回的带有html标签的富文本字符串
32 0
|
2月前
|
定位技术 开发工具 iOS开发
ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方案
ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方案
25 0
|
2月前
|
前端开发 Android开发 iOS开发
应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
【2月更文挑战第31天】应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
58 2
|
2月前
|
缓存
uni-app 82聊天页实时接收信息功能实现
uni-app 82聊天页实时接收信息功能实现
23 2
|
2月前
uni-app 79聊天类封装(十四)-处理接收消息
uni-app 79聊天类封装(十四)-处理接收消息
18 2