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

直接上代码:

//
// 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. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  2. load和ready

    <一>ready和load ready先执行,load后执行 DOM文档加载的步骤: () 解析HTML结构. () 加载外部脚本和样式表文件. () 解析并执行脚本代码. () 构造HT ...

  3. BZOJ 1008 越狱 (组合数学)

    题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...

  4. mySQL中replace的用法

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪   mysql replace实例说明: ...

  5. Curling 2.0(dfs回溯)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15567   Accepted: 6434 Desc ...

  6. Bug驱动开发(Bug-driven development)

    说实话,作为一个Domino开发者,像測试驱动开发(Test-driven development).功能驱动开发(Feature-driven development)之类软件开发的高大上的方法论( ...

  7. Your Job Is Not to Write Code

    I am lucky enough to work with a small team of fantastic engineers who truly care about their custom ...

  8. Leetcode解题记录

    尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...

  9. SharePoint BCS

    1. 开启相关的服务:管理中心-->应用程序管理-->管理服务器上的服务 2.

  10. Android学习之Drawable(一)

    Drawable有很多种,它们表示一种图像概念,但它们不全是图片.Drawable是什么呢?下面是Google Android API中的定义: A Drawable is a general abs ...