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

直接上代码:

//
// 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. C++如何将一个整数输出为小数

    double a=3;                      //一定是double不能是int cout.setf(ios::fixed); cout.precision(2); cout< ...

  2. JavaSE_ 集合框架 总目录(15~18)

    JavaSE学习总结第15天_集合框架1 15.01 对象数组的概述和使用15.02 对象数组的内存图解15.03 集合的由来及与数组的区别15.04 集合的继承体系图解15.05 Collectio ...

  3. 【转】CxImage图像库的使用

    CxImage下载地址:http://www.codeproject.com/KB/graphics/cximage/cximage600_full.zip 作者:Davide Pizzolato C ...

  4. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  5. Trie implementation

    在学习 Aho - Corasick Automation Algorithm 之前,先学习一下,Trie 的实现过程: The name trie comes from its use for re ...

  6. 怎么理解angularjs中的服务?

    AngularJS中的服务其实就是提供一种方式抽取共用类库 比如说一些工具类方法,我们传统的做法就是自己写个 utility 类,把相关的工具方法填充到utility里面去,最后把utility类放到 ...

  7. DropBox为什么一直那么红——靠用户体验,旗帜鲜明,它要保存的是你的重要随身资料,并且开放API

    链接:http://www.zhihu.com/question/19705960/answer/71742127来源:知乎 看到这个问题竟然从11年答到现在,有趣的是这几年里国内云存储行业变化也是蛮 ...

  8. 在VPS上安裝BT軟體Transmission

    在VPS上安裝BT軟體Transmission   作者: 窮苦人家的小孩 | 2009-12-04 55 Comments   VPS 還能怎玩?! 裝Proxy,裝VPN,這還不夠,我還用來掛種子 ...

  9. 实现最小宽度的几种方法及CSS Expression[转]

    实现最小宽度的几种方法及CSS Expression[转] 实现最小宽度的几种方法:css表达式尽量不用 支持FF IE7  IE6 .test { background:blue; min-widt ...

  10. Android学习笔记(十五)——碎片的生命周期(附源代码)

    碎片的生命周期 点击下载源代码 与活动类似.碎片具有自己的生命周期.理解了碎片的生命周期后.我们能够在碎片被销毁时正确地保存事实上例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Frag ...