iOS开发-本地通知

作者: FR_Zhang | 来源:发表于2016-08-30 19:51 被阅读1275次

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

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

如果写的还行,记得点赞哦

相关文章

  • iOS问题解决(三):模拟器收不到UILocalNotifica

    通知功能是iOS应用开发经常会碰到的需求,iOS应用的通知分为本地通知和远程通知(即消息推送),Apple dev...

  • iOS开发-本地通知

    这是我写的第一篇简书文章,写的不好,希望大家多多指导,多多交流. iOS的本地通知,多用于定时发送通知,比如游戏中...

  • iOS开发-本地通知

    这是我写的第一篇简书文章,写的不好,希望大家多多指导,多多交流. iOS的本地通知,多用于定时发送通知,比如游戏中...

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

  • ios开发之 本地通知

    在iOS开发中,通知也叫消息机制。消息机制包括两种,一种是本地通知,另一种是远程通知。本文我们着重理解本地通知,远...

  • iOS开发--本地通知与远程通知

    这里是指推送通知跟NSNotification有区别: 1、NSNotification是系统内部发出通知,一般用...

  • iOS开发--本地通知与远程通知

    作者雷潮关注 2016.02.01 00:18*字数 1921阅读 7038评论 1喜欢 35 这里是指推送通知跟...

  • iOS14开发- 通知

    iOS 中的通知主要分为 2 种,本地通知和远程通知。 本地通知 使用步骤 导入UserNotifications...

  • ios10新特性-UserNotification

    ios10新特性-UserNotification 引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是...

  • iOS UserNotifications通知管理---本地通知

    iOS UserNotifications通知管理---本地通知篇 iOS 10对以前混乱的和通知相关的API进行...

网友评论

    本文标题:iOS开发-本地通知

    本文链接:https://www.haomeiwen.com/subject/iofqettx.html