来自 金沙js77888 2020-04-28 21:40 的文章
当前位置: 金沙js77888 > 金沙js77888 > 正文

APP自动以iOS系统的通知的形式发送通知.

那是自家写的率先篇简书作品,写的糟糕,希望我们多多教导,多多调换.

iOS的地点文告,多用于准期发送布告,比方游戏中不足为道的下午十七点的体力领取的通报,吃药APP的准时提示等等,例子相当的少举了,简单的说,正是基于大家的供给,依照具体的一定的岁月段,APP自动以iOS系统的公告的花样发送文告.

上边就iOS当地文告做出详细的求证:

注:本地公告看作二个尤为重要的模块,这里开创八个地面布告的管理类:LocalNotificationManager.因为有些应用软件有写成单例类的供给,在程序中,增加了单例类的办法,仅供参谋,首要依旧以类方法施行操作:

 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 notification2 with an alert but haven't received permission from the user to display alerts3 with a sound but haven't received permission from the user to play sounds

为此本片文章重要针对iOS8之后做出的表明.首先决断当前客户对APP的关照权限,借使是首次运维软件,则会并发如下图的提拔,那么些是iOS系统自带的提醒选取方式,相信每一个iOS开垦的工程师都掌握的,具体操作,小编就相当少做表明了.

图片 1

在登记通知权限的事态,主如果iOS8事后版本的装置:

  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.设置文告的重要性参数这里关键是当地文告的参数设置,首要包蕴内容,触发时间等等

  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的倒计时,倒计时甘休后,施行业地文告

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

3State of Qatar.NSTimer执行的方法(这里是以秒为单位,每秒施行State of Qatar

- 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--;}

弹框的示例图:

图片 2

后台运维,发送文告的体裁:

图片 3

运行APP的样式:

图片 4

前段时间截至,发送公告的基本内容好似上所讲,至于拉长内容,大家温馨扩张.4.查看公告具体内容那部分也是最重视的片段,也是相比便于忽视的一部分,有如那一个涉及到应用软件的运作处境,所以,这里须要在应用软件Delegate中张开安装1卡塔尔国.首先在发送通告的还要,在应用软件上边相会世人格障碍最感冒的小1的注脚,所以,大家先是应超越淹没提示个数,

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

2卡塔尔(قطر‎.在App的代办中,在didReceiveLocalNotification中推行办法

- 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"];}

德姆o演示地址:

本文由金沙js77888发布于金沙js77888,转载请注明出处:APP自动以iOS系统的通知的形式发送通知.

关键词: 金沙娱乐网址 iOS 通知