zoukankan      html  css  js  c++  java
  • IM-iOS退出后台接受消息,app退出后台能接收到推送

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

    }

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

  • 相关阅读:
    >动态规划 4.26
    树链剖分+线段树求路径交
    PTA团体程序设计天梯赛-练习集 L2 网红点打卡攻略(模拟)
    PTA团体程序设计天梯赛-练习集 L2完全二叉树的层序遍历(递归)
    PTA团体程序设计天梯赛-练习集 L3-020 至多删三个字符 (dp)
    codeforces1509 D. Binary Literature (构造+指针)
    函数内容小结
    关于vim复制剪贴粘贴命令的总结-转
    GCC编译命令常用选项
    Ubuntu 和 windows1下文件夹共享的指令
  • 原文地址:https://www.cnblogs.com/PeterWolf/p/7886116.html
Copyright © 2011-2022 走看看