iOS创建本地通知和删除对应的通知,工作日通知
本文的代码主要是:创建本地通知,删除对应的本地通知,创建工作日闹钟
直接上代码:
//
// 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创建本地通知和删除对应的通知,工作日通知的更多相关文章
- iOS 创建本地私有库 保存功能代码
创建本地私有库 >>> cd /Users/cxx/Desktop/Mange_JJH/Lib >>> pod lib create TZTools >> ...
- Android 和iOS 创建本地通知
1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...
- iOS的本地推送删除不了解决方法
最近在研究苹果推送,当测试本地推送的时候,发现一个问题,就是一旦你添加了一个本地推动的通知,当你修改代码,删除应用,当你再次运行app,它还是会在横幅上面弹出推送,尼玛怎么搞都删除不了,近乎崩溃了,开 ...
- iOS --创建文件夹 ,删除文件夹
//创建文件夹 --> 返回 文件夹 - (NSString *)pathToPatientPhotoFolder { NSString *documentsDirectory = [NSSea ...
- iOS 图片本地存储、本地获取、本地删除
在iOS开发中.经常用到图片的本地化. iOS 图片本地存储.本地获取.本地删除,可以通过以下类方法实现. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...
- GitHub Desktop 如何创建本地仓库,上传代码,删除仓库
1.创建本地仓库 2.打开本地仓库,将要上传的文件放到本地仓库. 3.ctrl+p push仓库或者菜单栏Repository下push也可以用右上角的publish respository 4.左边 ...
- IOS 的本地通知
IOS 的本地通知 - (void)viewDidLoad { [super viewDidLoad]; UILocalNotification* localNotification = [[UILo ...
- iOS 电脑新装的系统, 使用sourceTree 创建本地仓库的时候, 总是提示, 无效路径
把qq聊天记录分享出来: 我电脑新装的系统, 使用sourceTree 创建本地仓库的时候, 总是提示, 无效路径请问哪位遇到过求指教群里有产品经理没有? ssh 配制的不对重装系统过后,重新生成一下 ...
- iOS开发本地通知
/* 本地通知:不通过网络,在本地实现的通知,自己发给自己 远程通知:必须通过网络,使用推送技术(APNs),实现通知 本地通知: 1.要完成可以接收的通知形式的注册 2.具体通知的设置 3.发送通知 ...
随机推荐
- BZOJ 3612: [Heoi2014]平衡( dp )
枚举Fl, 就变成一个整数划分的问题了...f(i,j) = f(i-j,j-1)+f(i-j,j)-f(i-N-1,j-1)递推.f(i,j)表示数i由j个不同的数组成,且最大不超过N的方案数 -- ...
- BZOJ 1207: [HNOI2004]打鼹鼠( dp )
dp.. dp[ i ] = max( dp[ j ] + 1 ) ------------------------------------------------------------------ ...
- BZOJ 1996: [Hnoi2010]chorus 合唱队(dp)
简单的dp题..不能更水了.. --------------------------------------------------------------- #include<cstdio&g ...
- idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class
idea中使用scala运行spark出现: Exception in thread "main" java.lang.NoClassDefFoundError: scala/co ...
- Java中String、StringBuilder以及StringBuffer
原文出处: 海子 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家一起学习一下String.StringBuilder和StringBuffe ...
- vmware 几种联网的方式,怎样实现虚拟机上网
我的pc有一个IP地址是可以訪问网络的,那么如何让VM可以共享我的IP地址,也能上网呢.今天在摸索中实现了,详细的配置例如以下: 1,首先将VM的网卡net8启用: 2,然后将VM的网卡设置为VMne ...
- 深入分析MySQL ERROR 1045 (28000)
这几天在MySQL新建用户后.出现訪问拒绝的问题,错误码为ERROR 1045(28000).在网上搜索了非常久.找到了非常多解决的方法,但非常遗憾的是这么多办法没有一个能解决该问题.尽管出现的错误码 ...
- Objective-c Category(类别)
category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...
- python第三方模块
python相关:1.zeromq网络库:2.twisted框架:twisted:一个基于事件驱动,异步的python高性能网络开发框架:注:什么是基于事件驱动:当(鼠标点击事件)事件注册器注入事件, ...
- 改变status bar的状态
两种改变status bar状态的方法 一 :(全局的) 直接在当前控制器中(一般是在navigationcontroller) //- (UIStatusBarStyle)preferredStat ...