除了第三方的推送,如友盟,极光,个推,信鸽等,还有本地的推送,本地推送一般用与从后台接口拿到数据进行推送或者本地定时提醒的推送。
关于本地推送的方法其实很简单,很多地方都有,博主这里就再贴一遍代码:

UILocalNotification *notification = [[UILocalNotification alloc] init];
//进行推送
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];

参数嘛,根据自己需要来加即可。

以上是推送的实现方法,但是关于推送消息,我们知道,会存在这么几种情况(场景:应用重启的时候请求接口,请求到数据设置推送,若是没有打开app,则接收不到推送消息):
1.存储的消息过期后怎么办;
2.接收的消息重复了怎么办;
3.消息怎么来存储;
!)那么先来说说这个消息怎么来存储:
博主这里采用userdefault来存储,先建立数据模型:

#import <Foundation/Foundation.h>

@interface LocalPushModel : NSObject

@property(nonatomic,retain)NSString *title;
@property(nonatomic,retain)NSString *msg;
@property(nonatomic,retain)NSString *image;
@property(nonatomic,retain)NSString *url;
@property(nonatomic,retain)NSString *icon;
@property(nonatomic,retain)NSString *send_time;
@property(nonatomic,retain)NSString *user_sid;
@property(nonatomic,retain)NSNumber *msg_id;

-(instancetype)initWithDic:(NSDictionary *)dic;

@end




#import "LocalPushModel.h"

@implementation LocalPushModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

}
//这里为什么要加下面的两个方法呢?因为userdefault不能存储对象,这里是以模型
的形式存进去,也算是对象了,所以直接存储会造成崩溃。
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_title forKey:@"title"];
[aCoder encodeObject:_msg forKey:@"msg"];
[aCoder encodeObject:_image forKey:@"image"];
[aCoder encodeObject:_url forKey:@"url"];
[aCoder encodeObject:_icon forKey:@"icon"];
[aCoder encodeObject:_send_time forKey:@"send_time"];
[aCoder encodeObject:_user_sid forKey:@"user_sid"];
[aCoder encodeObject:_msg_id forKey:@"msg_id"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.title = [aDecoder decodeObjectForKey:@"title"];
self.msg = [aDecoder decodeObjectForKey:@"msg"];
self.image = [aDecoder decodeObjectForKey:@"image"];
self.url = [aDecoder decodeObjectForKey:@"url"];
self.icon = [aDecoder decodeObjectForKey:@"icon"];
self.send_time = [aDecoder decodeObjectForKey:@"send_time"];
self.user_sid = [aDecoder decodeObjectForKey:@"user_sid"];
self.msg_id = [aDecoder decodeObjectForKey:@"msg_id"];
}
return self;
}
@end

!!)拿到数据后要怎么处理呢:

+ (void)dealLocalPush:(NSArray *)pushInforArray {
NSArray *tmpArray = [NSArray arrayWithArray:pushInforArray];
UILocalNotification *notification = [[UILocalNotification alloc] init];

NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
if (![userdefault objectForKey:@"localPush"]) {
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"];
notification.userInfo = dUserInfo;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
[userdefault setValue:data forKey:@"localPush"];
}
else
{
//读取userdefault归档数据
NSData *encodeObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"localPush"];
NSMutableArray *pushArray = [NSKeyedUnarchiver unarchiveObjectWithData:encodeObject];
NSMutableArray *pushSaveArray = [[NSMutableArray alloc] init];

//取出未过期的消息
for (int i = 0; i < pushArray.count; i++) {
LocalPushModel *localpushModel = pushArray[i];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [NSDate date];
NSDate *pushDate = [formatter dateFromString:localpushModel.send_time];
int cha = [pushDate timeIntervalSinceDate:date];
if (cha > 0) {
[pushSaveArray addObject:localpushModel];
}
}

//过滤之后留下的新的推送消息,添加到一个新的数组中准备进行推送
NSMutableArray *newAddArray = [[NSMutableArray alloc] init];
for (int i = 0; i < tmpArray.count; i++) {
LocalPushModel *newPushModel = tmpArray[i];
int num = 0;
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.alertTitle = [NSString stringWithFormat:@"%@",newPushModel.title];
notification.alertBody = [NSString stringWithFormat:@"%@",newPushModel.msg];
notification.alertLaunchImage = [NSString stringWithFormat:@"%@",newPushModel.image];
notification.alertAction = NSLocalizedString(@"查看", nil);
NSMutableDictionary *dUserInfo = [[NSMutableDictionary alloc] init];
[dUserInfo setObject:@"3" forKey:@"type"];
[dUserInfo setObject:newPushModel.url forKey:@"value"];
notification.userInfo = dUserInfo;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}
}
[pushSaveArray addObjectsFromArray:newAddArray];

//本地归档
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:pushSaveArray];
[userdefault setValue:data forKey:@"localPush"];
}

[userdefault synchronize];
}

详情呢,请看里面注释,写得都很详细,这样的话可基本实现推送的功能,但是存在一点瑕疵,如果推送功能关闭了怎么办?依然保存了,但是当再打开推送的时候,同样的消息发过来,本地已经存储,默认为已经写进推送,但实际上写入是失败的,新的一样的消息过来,会在去重的时候过滤掉,那就尴尬了。所以要完善此功能博主提供两种方案:
1)直接在模型里增加isPush字段,通过判断推送开关是否打开在存储时进行赋值保存,此方案不再在上面基础上增加代码,可自行加上。(分析:虽然有这种可能,但是总的来说影响并不是很大,这种情况的出现概率比较低,简单使用,以上功能足以实现基本需求,上面的方法还有一个好处就是,如果需要在某处显示消息,可从userdefault中取出来进行处理);
2)直接操作推送的队列,具体请看下一篇博客:​​iOS开发-本地推送实现方法和数据处理方案(二)​​