IOS 的本地通知

- (void)viewDidLoad

{

[super viewDidLoad];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];

if(localNotification)

{

NSDate *now=[NSDate date];

localNotification.timeZone = [NSTimeZonedefaultTimeZone];

localNotification.repeatInterval =NSDayCalendarUnit;

localNotification.applicationIconBadgeNumber = 1;

localNotification.alertBody=@"该去吃晚饭了!";

localNotification.alertAction =NSLocalizedString(@"显示", nil);

NSDictionary* dic = [NSDictionarydictionaryWithObject:@"123" forKey:@"key"];

[localNotification setUserInfo:dic];

localNotification.fireDate = [nowdateByAddingTimeInterval:10];

//    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

localNotification.hasAction = YES;

//        localNotification.repeatInterval = NSMinuteCalendarUnit;

localNotification.soundName =UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

[localNotification release];

}

//得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:

NSDate* yesterday = [NSDatedateWithTimeIntervalSinceNow:-(24 * 60 * 60)];

NSLog(@"yesterday:%@",yesterday);

NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];

[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

//计算 两个时间相隔多少秒

NSDate* date1 = [dateFormat dateFromString:[dateFormatstringFromDate:[NSDatedateWithTimeIntervalSinceNow:60*60*8]]];

NSLog(@"date1 = %@",date1);

NSDate* date2 =[NSDate dateWithTimeInterval:60*60*8sinceDate:[dateFormat dateFromString:@"2013-10-12 11:08:33"]] ;

NSLog(@"date2 = %@",date2);

NSTimeInterval timerInterVal = [date2timeIntervalSinceDate:date1];

NSLog(@"timerInterVal  %f",timerInterVal);

}

//监听本地通知事件:退出程序后执行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]] autorelease];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

ViewController* view = [[ViewController alloc] init];

[self.window setRootViewController:view];

[view release];

//设置iCon 上的数字

application.applicationIconBadgeNumber = 0;

UILocalNotification* notification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsAnnotationKey];

if(notification)

{

NSLog(@"didFinishLaunchingWithOptions");

UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:nilmessage:@"received E-mail" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];

[alert show];

[alert release];

}

[self.window makeKeyAndVisible];

return YES;

}

//监听本地通知事件:在没有退出程序的时候执行

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

NSLog(@"%@",[notification.userInfo valueForKey:@"key"]);

if(application.applicationState == UIApplicationStateActive)

{

UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@""

message:@"事项提醒"

delegate:self

cancelButtonTitle:@"关闭"

otherButtonTitles:nil, nil] autorelease];

[alert show];

}

else

{

[[UIApplication sharedApplication] cancelAllLocalNotifications];

}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

[[UIApplication sharedApplication] cancelAllLocalNotifications];

}

 

下面是写在类里面的

+(void)addLocalNotificationWithMessage:(NSString *)message

FireDate:(NSDate *) fireDate

AlarmKey:(NSString *)alarmKey

{

UILocalNotification *notification=[[UILocalNotification alloc] init];

if (notification!=nil) {

notification.fireDate=fireDate;

notification.timeZone=[NSTimeZone defaultTimeZone];

notification.soundName= UILocalNotificationDefaultSoundName;

notification.alertBody=message;

notification.hasAction = NO;

notification.userInfo=[[NSDictionary alloc]initWithObjectsAndKeys:alarmKey,@"AlarmKey", nil];

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

[notification release];

}

+(void)deleteLocalNotification:(NSString *) alarmKey

{

NSArray * allLocalNotification=[[UIApplication sharedApplication]scheduledLocalNotifications];

for (UILocalNotification * localNotification in allLocalNotification) {

NSString * alarmValue=[localNotification.userInfoobjectForKey:@"AlarmKey"];

if ([alarmKey isEqualToString:alarmValue]) {

[[UIApplication sharedApplication]cancelLocalNotification:localNotification];

}

}

}

IOS 的本地通知的更多相关文章

  1. Android 和iOS 创建本地通知

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

  2. iOS中 本地通知/本地通知详解 韩俊强的博客

    布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 Notification是智能手机应用编 ...

  3. IOS开发-本地通知

    // 注册 发送通知的方法 -(void)pushNotfation{ //--------------初始化本地通知 alloc init 虽然是UI控件 但继承NSObject UILocalNo ...

  4. iOS创建本地通知和删除对应的通知,工作日通知

    本文的代码主要是:创建本地通知,删除对应的本地通知,创建工作日闹钟 直接上代码: // // ViewController.m // LocalNSNotification // // Created ...

  5. iOS开发本地通知

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

  6. IOS本地通知:UILocalNotification使用记录

    第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...

  7. iOS 10 UserNotification框架解析 – 本地通知

    iOS 10以前的通知比较杂乱,把本地通知和远程通知分开了,诞生了许多功能类似的API,很容易让初学者犯迷糊.而iOS 10的通知把API做了统一,利用独立的UserNotifications.fra ...

  8. cocos2d-js 安卓自定义本地通知功能

    安卓新手,笔记有理解不当的地方望指出,经过几天折腾终于可以实现类似ios的本地通知功能(ios就几行代码),可能有第三方sdk可以方便实现,暂时没去找 思路: 1. startService 和bin ...

  9. iOS(本地通知与远程通知)

    iOS 推送通知有两种:本地推送.远程推送. 本地推送 :  在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟.本地通送有局限性在于当APP处于后台或者退出时就无法发出通知. 远程 ...

随机推荐

  1. 分析Ubuntu18.04启动后的各种任务

    jello@jello:~$ ps -A  PID TTY          TIME CMD    1 ?        00:00:02 systemd    由idle进程(进程号为0的进程,那 ...

  2. [洛谷P4918]信仰收集

    题目背景 随着各种势力的迁入,守矢神社丧失了不少信仰现在,为了挽回香火日益惨淡的神社,八坂神奈子派遣神社的风祝早苗去人类村落收集信仰 题目描述 你可以将村落看成一个m个点的有向无环图(编号从1−m), ...

  3. Student : IComparable<Student> 以及逆变和协变

    IComparable<Student>是Student的父类,所以IComparable<Student>可以接收Student.但是在使用CompareTo方法的时候,必须 ...

  4. geoserver源码学习与扩展——restAPI访问

    产生这篇文章的想法是在前端通过js调用restAPI时,总是不成功,发送ajax请求时还总是出现类似跨域的问题,后来查找才发现,默认情况下restAPI的访问都需要管理员权限,而通过ajax请求传输用 ...

  5. WinCE数据通讯之Web Service分包传输篇

    前面写过<WinCE数据通讯之Web Service篇>那篇对于数据量不是很大的情况下单包传输是可以了,但是对于大数据量的情况下WinCE终端的内存往往会在解包或者接受数据时产生内存溢出. ...

  6. 全文检索引擎Solr系列——整合中文分词组件mmseg4j

    默认Solr提供的分词组件对中文的支持是不友好的,比如:“VIM比作是编辑器之神”这个句子在索引的的时候,选择FieldType为”text_general”作为分词依据时,分词效果是: 它把每一个词 ...

  7. JavaScript内存泄漏知多少?

    垃圾回收解放了我们,它让我们可将精力集中在应用程序逻辑(而不是内存管理)上.但是,垃圾收集并不神奇.了解它的工作原理,以及如何使它保留本应在很久以前释放的内存,就可以实现更快更可靠的应用程序.在本文中 ...

  8. Bootstrap 与 IE 兼容模式 关系讲解

    IE 兼容模式 Bootstrap 不支持 IE 古老的兼容模式.为了让 IE 浏览器运行最新的渲染模式下,建议将此 <meta> 标签加入到你的页面中:Copy <meta htt ...

  9. 2018.2.2IDEA 项目层级问题

    这个问题也不是太大,个人爱好,老是觉着影响美观.网上找了一下:[Hide Empty Middle Packages]说是把这个打勾的去掉就可以了. 但是:我的这个好像没有. 这样点击小齿轮把第二个C ...

  10. Emacs Helm: 使用关键字搜索、获取、执行任何东西

    Helm 是一个emacs的软件包,定义了一个通用框架,交互式地.动态缩减式地使用关键字选择.获取.执行任何东西.比如: 执行emacs 命令 打开文件 查看man文档 执行grep操作 执行apt命 ...