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

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

除了第三方的推送,如友盟,极光,个推,信鸽等,还有本地的推送,本地推送一般用与从后台接口拿到数据进行推送或者本地定时提醒的推送。


关于本地推送的方法其实很简单,很多地方都有,博主这里就再贴一遍代码:

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开发-本地推送实现方法和数据处理方案(二)

目录
相关文章
|
8月前
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
474 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
7月前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
217 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
9月前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
281 66
|
6月前
|
iOS开发 开发者 Windows
uniapp云打包ios应用证书的获取方法,生成指南
打包用到的一共两个文件,一个是p12格式的私钥证书,一个是证书profile文件。其中生成p12证书的时候,按照官网的教程,是需要MAC电脑来协助做的,主要是生成一些csr文件和导出p12证书等。其实这些步骤也可以借助一些其他的工具来实现,不一定使用mac电脑,用windows电脑也可以创建。
870 0
|
7月前
|
人工智能 程序员 API
iOS|记一名 iOS 开发新手的前两次 App 审核经历
啥,这玩意也有新手保护期?
141 0
|
9月前
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
842 11
|
10月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
9月前
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
298 3
|
9月前
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
10月前
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。