本文的代码主要是:创建本地通知,删除对应的本地通知,创建工作日闹钟

直接上代码:

//
// ViewController.m
// LocalNSNotification
//
// Created by wusiping on 16/1/27.
// Copyright © 2016年 wusiping. All rights reserved.
// #import "ViewController.h"
#define LOCAL_NOTIFY_SCHEDULE_ID @"hahahhahahhah" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIButton *createBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
[createBtn setTitle:@"创建通知" forState:UIControlStateNormal];
createBtn.titleLabel.textColor = [UIColor blackColor];
createBtn.backgroundColor = [UIColor lightGrayColor];
[createBtn addTarget:self action:@selector(createNSNotification) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:createBtn]; UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
[cancelBtn setTitle:@"删除通知" forState:UIControlStateNormal];
cancelBtn.titleLabel.textColor = [UIColor blackColor];
cancelBtn.backgroundColor = [UIColor lightGrayColor];
[cancelBtn addTarget:self action:@selector(cancelNSNotification) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelBtn]; } - (NSDate *)getCurrrentTimeToSS
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter dateFormat];
[NSDate date];
return [NSDate date]; } - (void)createNSNotification{ UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) {
return;
}
//设置本地通知的触发时间(如果要立即触发,无需设置),这里设置为20妙后
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
//设置本地通知的时区
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//设置通知的内容
localNotification.alertBody = @"hahhah";
//设置通知动作按钮的标题
localNotification.alertAction = @"查看";
//设置提醒的声音,可以自己添加声音文件,这里设置为默认提示声
localNotification.soundName = UILocalNotificationDefaultSoundName;
//设置通知的相关信息,这个很重要,可以添加一些标记性内容,方便以后区分和获取通知的信息
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:],@"nfSignInkey",nil];
[localNotification setUserInfo:dict]; // ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
localNotification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重复提示的单位,可以是天、周、月
localNotification.repeatInterval = NSDayCalendarUnit;
} //在规定的日期触发通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } /**
* 删除当前的通知
*/ - (void)cancelNSNotification{ // 手动删除通知
// 这里我们要根据我们添加时设置的key和自定义的ID来删
NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications];
NSUInteger acount=[narry count];
if (acount>)
{
// 遍历找到对应nfkey和notificationtag的通知
for (int i=; i<acount; i++)
{
UILocalNotification *myUILocalNotification = [narry objectAtIndex:i];
NSDictionary *userInfo = myUILocalNotification.userInfo;
NSNumber *obj = [userInfo objectForKey:@"nfSignInkey"];
int mytag=[obj intValue];
if (mytag==)
{
// 删除本地通知
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
break;
}
}
}
} @end 大家可能会遇到工作日闹钟这种需求:
 if ([cycle isEqual:@"工作日"]) {//如果是工作日闹钟
NSDate *now=select;//当前时间
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [gregorian components:NSWeekdayCalendarUnit fromDate:now];
NSInteger daycount = [dateComps weekday]-;
NSDate *weekdaybegin=[now addTimeInterval:-daycount***];
for (int i=;i<;i++) {//创建5个通知:星期一到星期五
NSDate *now= [weekdaybegin addTimeInterval:i***];
[self creatmessage:now];
}
} -(void)creatmessage:(NSDate *)new
{
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil && select!=nil &&cycle.length > &&key.intValue==)
{
//触发通知时间
//[self cancelUILocalNotification];
NSDate *now= new;
NSLog(@"%@",now);
NSDate *today = [self getCurrrentTimeToDay];
BOOL isSameDay = [self isSameDay:now date2:today];
if (isSameDay == YES){//特殊需求..这个不要管
//今天所在的星期X,延迟一个礼拜触发。比如今天是星期三,那下个礼拜三才开始闹钟。明天是星期四,星期四的闹钟还是明天就触发 now = [NSDate dateWithTimeInterval:*** sinceDate:now]; }
notification.fireDate=now;
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval = kCFCalendarUnitWeek;
notification.soundName=@"铃声.m4r";
notification.alertBody=@"今天iSite签到没?千万不要忘了哦!";
notification.alertAction=NSLocalizedString(@"今天iSite签到没?千万不要忘了哦!", nil);
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:],@"nfSignInkey",nil];
[notification setUserInfo:dict]; // 启用这个通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
} }

 

iOS创建本地通知和删除对应的通知,工作日通知的更多相关文章

  1. iOS 创建本地私有库 保存功能代码

    创建本地私有库 >>> cd /Users/cxx/Desktop/Mange_JJH/Lib >>> pod lib create TZTools >> ...

  2. Android 和iOS 创建本地通知

    1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...

  3. iOS的本地推送删除不了解决方法

    最近在研究苹果推送,当测试本地推送的时候,发现一个问题,就是一旦你添加了一个本地推动的通知,当你修改代码,删除应用,当你再次运行app,它还是会在横幅上面弹出推送,尼玛怎么搞都删除不了,近乎崩溃了,开 ...

  4. iOS --创建文件夹 ,删除文件夹

    //创建文件夹 --> 返回 文件夹 - (NSString *)pathToPatientPhotoFolder { NSString *documentsDirectory = [NSSea ...

  5. iOS 图片本地存储、本地获取、本地删除

    在iOS开发中.经常用到图片的本地化. iOS 图片本地存储.本地获取.本地删除,可以通过以下类方法实现. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...

  6. GitHub Desktop 如何创建本地仓库,上传代码,删除仓库

    1.创建本地仓库 2.打开本地仓库,将要上传的文件放到本地仓库. 3.ctrl+p push仓库或者菜单栏Repository下push也可以用右上角的publish respository 4.左边 ...

  7. IOS 的本地通知

    IOS 的本地通知 - (void)viewDidLoad { [super viewDidLoad]; UILocalNotification* localNotification = [[UILo ...

  8. iOS 电脑新装的系统, 使用sourceTree 创建本地仓库的时候, 总是提示, 无效路径

    把qq聊天记录分享出来: 我电脑新装的系统, 使用sourceTree 创建本地仓库的时候, 总是提示, 无效路径请问哪位遇到过求指教群里有产品经理没有? ssh 配制的不对重装系统过后,重新生成一下 ...

  9. iOS开发本地通知

    /* 本地通知:不通过网络,在本地实现的通知,自己发给自己 远程通知:必须通过网络,使用推送技术(APNs),实现通知 本地通知: 1.要完成可以接收的通知形式的注册 2.具体通知的设置 3.发送通知 ...

随机推荐

  1. java 成员访问修饰符

    作用域 当前类 当前包(package) 子类 其他包(package) public ok ok ok ok protected ok ok ok no default ok ok no no pr ...

  2. 浏览器 窗口 scrollTop 的兼容性问题

    window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...

  3. select标签操作大全

    http://blog.csdn.net/hhhh2012/article/details/8610336

  4. javascript 未结束的字符串常量

    1.JAVASCRIPT引用时,使用的字符语言不一致. 比如:<script type=”text/javascript” src=”xxx.js” charset=”UTF-8″>.xx ...

  5. javascript 作用域链

    最近想整理一下js执行代码的一些知识,如果有出错的地方还请指正. 执行环境(Execution Context) 所有的javascript代码都是在一个执行环境中被执行的.它只是一种机制,用来完成运 ...

  6. 手动修改VisualStudio IISExpress的配置

    <VisualStudio> <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> ...

  7. 国内BI工具/报表工具厂商简介

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  8. QString类的使用(无所不包,极其方便)

    Qt的QString类提供了很方便的对字符串操作的接口. 使某个字符填满字符串,也就是说字符串里的所有字符都有等长度的ch来代替. QString::fill ( QChar ch, int size ...

  9. 通往WinDbg的捷径

    通往WinDbg的捷径(一) 原文:http://www.debuginfo.com/articles/easywindbg.html译者:arhat时间:2006年4月13日关键词:CDB WinD ...

  10. USACO 2001 OPEN

    第1题 绿组. 奶牛接力赛[relay] 题目描述 农夫约翰已经为一次赛跑选出了K(2≤K≤40)头牛组成了一支接力队.赛跑在农夫约翰所拥有的农场上进行,农场的编号为1到Ⅳf4≤Ⅳ< 800), ...