开发者社区> CodingFire> 正文

iOS开发-本地推送实现方法和数据处理方案(二)

简介: iOS开发-本地推送实现方法和数据处理方案(二)
+关注继续查看

上一篇中最后提到一个缺陷,然后用读取推送队列中消息的方式来管理推送。


读取推送队列中所有的本地推送(和第三方的推送无关,不要混淆):

    NSArray *notiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];

怎么把推送重新存储到本地推送的队列呢:

        [UIApplication sharedApplication].scheduledLocalNotifications = notiNeedArray;

然后对于数据的处理其实和上一篇一样,变的地方有两个:


1)数据的存储问题;


2)如果存在其他本地推送,在不影响其他本地推送的情况下来处理此类本地推送;


声明:这里的方法使用的模型和上一篇一样,此处不再重复贴代码


下面来说明下对数据的处理方案:

+ (void)dealLocalPush:(NSArray *)pushInforArray {
    NSArray *tmpArray = [NSArray arrayWithArray:pushInforArray];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    //取出本地推送的队列中的推送消息(要知道一点,取出的是所有的本地推送记录,包括已经过期的,只要是在本地推送过的或者即将推送的,都可以取出来)
    NSArray *notiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];

    if (notiArray.count > 0) {
        NSMutableArray *notiArrayCopy = [NSMutableArray arrayWithArray:notiArray];
        NSMutableArray *notiNeedArray = [[NSMutableArray alloc] init];
        //取出未过期的消息
        for (int i = 0; i < notiArrayCopy.count; i++) {
            UILocalNotification *notiDic = notiArrayCopy[i];
            //这里要留意,取出的未过期的消息包括其他本地推送,直接取出,不作任何处理,以保证我们不影响以他本地推送的宗旨
            if (![notiDic.userInfo objectForKey:@"msg_id"]) {
                [notiNeedArray addObject:notiDic];
            }
            else
            {
                //下面的方法为对比时间获取未过期的推送,推送时间与当前时间的差值,大于0为还没到时间,等于0为到时间,开始推送,小于0为过期推送,可以用到的地方很多,如秒杀等。
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSDate *date = [NSDate date];
                int cha = [notiDic.fireDate timeIntervalSinceDate:date];
                if (cha > 0) {
                    [notiNeedArray addObject:notiDic];
                }
            }
        }
        //过滤掉过期的推送后重新存入本地推送队列
        [UIApplication sharedApplication].scheduledLocalNotifications = notiNeedArray;
        //存好后取出本地推送
        notiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    }

    //若本地推送队列中无本地推送
    if (notiArray.count == 0) {
        for (int i = 0; i < tmpArray.count; i++) {
        //下面是对消息的本地推送,上一篇博客中有提到,比较简单,参数要注意下。
            LocalPushModel *localpushModel = tmpArray[i];
            NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
            NSDate *date =[dateFormat dateFromString:localpushModel.send_time];
            //进行推送
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.fireDate = date;
            notification.alertTitle = [NSString stringWithFormat:@"%@",localpushModel.title];
            notification.alertBody = [NSString stringWithFormat:@"%@",localpushModel.msg];
            notification.alertLaunchImage = [NSString stringWithFormat:@"%@",localpushModel.image];
            notification.alertAction = NSLocalizedString(@"查看", nil);
            NSMutableDictionary *dUserInfo = [[NSMutableDictionary alloc] init];
            [dUserInfo setObject:@"3" forKey:@"type"];
            [dUserInfo setObject:localpushModel.url forKey:@"value"];
            [dUserInfo setObject:localpushModel.user_sid forKey:@"user_sid"];
            [dUserInfo setObject:localpushModel.msg_id forKey:@"msg_id"];
            if (localpushModel.icon == nil) {
                [dUserInfo setObject:@"icon" forKey:@"icon"];
            }
            notification.userInfo = dUserInfo;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
    }
    else
    {
        //读取通知数据并过滤userInfor中没有msg_id的数据
        NSMutableArray *pushSaveArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < notiArray.count; i++) {
            UILocalNotification *notiDic = notiArray[i];
            //这里需要根据特殊字段区分是哪一种本地推送,只操作我们需要的数据,你可以直接来操作取出来的这个数组,也可以像博主一样转模型后来操作
            if ([notiDic.userInfo objectForKey:@"msg_id"]) {
                LocalPushModel *localpushModel = [[LocalPushModel alloc] init];
                localpushModel.title = [NSString stringWithFormat:@"%@",notiDic.alertTitle];
                localpushModel.msg = [NSString stringWithFormat:@"%@",notiDic.alertBody];
                localpushModel.image = [NSString stringWithFormat:@"%@",notiDic.alertLaunchImage];
                localpushModel.url = [NSString stringWithFormat:@"%@",[notiDic.userInfo objectForKey:@"value"]];
                localpushModel.icon = [NSString stringWithFormat:@"%@",[notiDic.userInfo objectForKey:@"icon"]];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *strDate = [dateFormatter stringFromDate:notiDic.fireDate];
                localpushModel.send_time = [NSString stringWithFormat:@"%@",strDate];
                localpushModel.user_sid = [NSString stringWithFormat:@"%@",[notiDic.userInfo objectForKey:@"user_sid"]];
                localpushModel.msg_id = [notiDic.userInfo objectForKey:@"msg_id"];
                [pushSaveArray addObject:localpushModel];
            }
        }

        //过滤新请求的推送消息,进行去重(上面已经把过期的消息处理掉了),添加到一个新的数组中准备进行推送
        NSMutableArray *newAddArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < tmpArray.count; i++) {
            LocalPushModel *newPushModel = tmpArray[i];
            int num = 0;
            //拿新的消息和本地存储的消息一一比较,根据num,只要有msg_id相等就代表是同一条推送,num就会➕1,那这样的话就不会加入到‘需要推送的数组’
            for (int j = 0; j < pushSaveArray.count; j++) {
                LocalPushModel *localpushModel = pushSaveArray[j];
                if (newPushModel.msg_id == localpushModel.msg_id) {
                    num++;
                }
            }
            if (num == 0) {
                [newAddArray addObject:newPushModel];
            }
        }

        //上面拿到的是新消息中不重复的消息,需要对新的消息进行推送和保存
        if (newAddArray.count > 0) {
            for (int i = 0; i < newAddArray.count; i++) {
                LocalPushModel *newPushModel = newAddArray[i];
                //推送
                NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
                [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
                NSDate *date =[dateFormat dateFromString:newPushModel.send_time];
                //进行推送
                notification.timeZone = [NSTimeZone defaultTimeZone];
                notification.fireDate = date;
                notification.alertAction = NSLocalizedString(@"查看", nil);
                NSMutableDictionary *dUserInfo = [[NSMutableDictionary alloc] init];
                notification.alertBody = [NSString stringWithFormat:@"%@",newPushModel.msg];
                notification.alertTitle = [NSString stringWithFormat:@"%@",newPushModel.title];
                notification.alertLaunchImage = [NSString stringWithFormat:@"%@",newPushModel.image];
                [dUserInfo setObject:@"3" forKey:@"type"];
                [dUserInfo setObject:newPushModel.url forKey:@"value"];
                [dUserInfo setObject:newPushModel.msg_id forKey:@"msg_id"];
                [dUserInfo setObject:newPushModel.user_sid forKey:@"user_sid"];
                if (newPushModel.icon == nil) {
                    [dUserInfo setObject:@"icon" forKey:@"icon"];
                }
                notification.userInfo = dUserInfo;
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            }
        }
    }
}

关于推送,尤其需要注意userInfor,和普通字典一样的,不能存储一个为nil的值。


代码的注释,每一步都写的很清楚了,看到这里,相信你已经学会了怎么来进行最佳的本地推送,觉得不错就留下你的评论吧。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
68 0
iOS开发-本地推送实现方法和数据处理方案(一)
iOS开发-本地推送实现方法和数据处理方案(一)
55 0
iOS开发 - 不通过import引入类名实现push或present
iOS开发 - 不通过import引入类名实现push或present
15 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
114 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
72 0
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
56 0
iOS开发 - 用AFNetworking实现https单向验证,双向验证
iOS开发 - 用AFNetworking实现https单向验证,双向验证
90 0
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
43 0
iOS小技能:【发红包】使用tweak和lua脚本结合进行实现
我们开发的大部分越狱程序,都是编译成动态链接库(`例如:介绍的越狱程序(Tweak)开发,就是动态链接库。`),然后通过越狱平台的MobileSubstrate(iOS7上叫CydiaSubstrate)来加载进入目标程序(Target),通过对目标程序的挂钩(Hook),来实现相应的功能。
64 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Facebook iOS App技术演化十年之路
立即下载
From Java_Android to Swift iOS
立即下载
深入剖析 iOS 性能优化
立即下载