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

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

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的值。

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