iOS开发--本地通知

这是我写的第一篇简书文章,写的不好,希望大家多多指导,多多交流.

iOS的本地通知,多用于定时发送通知,比如游戏中常见的中午十二点的体力领取的通知,吃药APP的定时提醒等等,例子不多举了,总之,就是根据大家的需求,根据具体的特定的时间段,APP自动以iOS系统的通知的形式发送通知.

下面就iOS本地通知做出详细的说明:

注:本地通知作为一个重要的模块,这里创建一个本地通知的管理类:LocalNotificationManager.因为部分APP有写成单例类的需求,在程序中,添加了单例类的方法(多线程创建方式),仅供参考,主要还是以类方法执行操作:

 static LocalNotificationManager * shareManager = nil;
 + (LocalNotificationManager *)shareMG {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[LocalNotificationManager alloc]init];
    });
    return shareManager;
}

1.注册本地通知
在iOS8之后,以前的本地推送写法可能会出错,接收不到推送的信息,
如果出现以下信息:
1 Attempting to schedule a local notification
2 with an alert but haven’t received permission from the user to display alerts
3 with a sound but haven’t received permission from the user to play sounds

因此本片文章主要针对iOS8之后做出的说明.
首先判断当前用户对APP的通知权限,如果是首次运行软件,则会出现如下图的提示,这个是iOS系统自带的提示选择方式,相信每个iOS开发的程序员都知道的,具体操作,我就不多做解释了.

在注册通知权限的情况,主要是iOS8之后版本的设置:

+ (void)registLocalNotification {
//创建本地通知对象
UILocalNotification * localNotif = [[UILocalNotification alloc]init];
//判断本地通知中是否已经注册过通知了
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// 通知的类型,设置声音,弹框等等......
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//执行通知注册
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
localNotif.repeatInterval = NSCalendarUnitDay;
}else {
// 通知重复提示的单位,可以是天、周、月
localNotif.repeatInterval = NSDayCalendarUnit;
}
}

2.设置通知的重要参数
这里主要是本地通知的参数设置,主要包括内容,触发时间等等

+ (void)setLocalNotificationWithAlertBody:(NSString *)alertBody alertTime:(NSInteger)alertTime noticeStr:(NSString *)str {
UILocalNotification * localNotification = [[UILocalNotification alloc]init];
//设置出发通知的时间
NSDate * date = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"---%@", date);
localNotification.fireDate = date;
// 设置时区
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复的间隔
localNotification.repeatInterval = kCFCalendarUnitSecond;
// 设置通知的内容
localNotification.alertBody = alertBody;
localNotification.applicationIconBadgeNumber = 1;
// 通知时播放声音
localNotification.soundName = UILocalNotificationDefaultSoundName;
// 通知参数,将内容通过通知携带
NSDictionary * dic = [NSDictionary dictionaryWithObject:str forKey:@"localNotification"];
localNotification.userInfo = dic;
// 将通知添加到系统中
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

3.本地通知的实现
这里测试系统的可行性,我们可以通过本地通知与NSTimer结合,从而更形象化的进行说明.
1).首先注册通知:

[LocalNotificationManager registLocalNotification];

2).点击事件触发NSTimer的倒计时,倒计时结束后,执行本地通知

- (IBAction)sendLocalNotification:(UIButton *)sender {
//设置倒计时的总时间
timeNumber = 5;
//设置倒计时
NSTimer * timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reloadTimeBtn:) userInfo:nil repeats:YES];
//倒计时执行
[timer fire];
}

3).NSTimer执行的方法(这里是以秒为单位,每秒执行)

- (void)reloadTimeBtn:(NSTimer *)sender {
[self.button setTitle:[NSString stringWithFormat:@"%ld", timeNumber] forState:UIControlStateNormal];
if (timeNumber < 0) {
//time为0的时候执行通知
[self.button setTitle:[NSString stringWithFormat:@"完成"] forState:UIControlStateNormal];
[sender invalidate];
//执行通知,设置参数
//body  弹框的标题
//noticeStr  弹框的主要内容
[LocalNotificationManager setLocalNotificationWithAlertBody:@"爆炸啦" alertTime:0 noticeStr:@"Boom!!沙卡拉卡"];
}
timeNumber--;
}

弹框的示例图:

后台运行,发送通知的样式:

运行APP的样式:

目前为止,发送通知的基本内容就如上所讲,至于丰富内容,大家自己扩展.
4.查看通知具体内容
这部分也是最重要的部分,也是比较容易忽略的部分,犹如这个涉及到APP的运行状态,所以,这里需要在APPDelegate中进行设置
1).首先在发送通知的同时,在APP上面会出现强迫症最讨厌的小1的标志,所以,我们首先应该先消除提醒个数,

- (void)applicationDidBecomeActive:(UIApplication *)application {
//清空提醒的个数
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

2).在App的代理中,在didReceiveLocalNotification中执行方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//获取通知信息
NSString * messageNoti = [notification.userInfo objectForKey:@"localNotification"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"爆炸啦" message:messageNoti delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
// 更新显示的徽章个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
// 在不需要再推送时,可以取消推送
[LocalNotificationManager cancelLocalNotificationWithKey:@"key"];
}

Demo演示地址:
https://github.com/zhangfurun/FRLocalNotificationDemo.git
如果写的还行,记得点赞哦

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值