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编译openwrt前的准备

    1.获取openwrt源码 git clone https://github.com/openwrt/openwrt.git 2.安装一些库及必备程序: sudo apt-get install li ...

  2. hadoop项目实战--ETL--(一)项目分析

    项目描述 一 项目简介 在远程服务器上的数据库中有两张表,user 和order,现需要对表中的数据做分析,将分析后的结果再存到mysql中.两张表的结构如下图所示 现需要分析每一天user和,ode ...

  3. Objective C NSString 编码成URL 特殊字符处理

    找了一下网上的教程都是使用类似以下代码,Xcode提示这个CoreFoundation不受ARC管理,所以折中的方式是添加__bridge. NSString *encodedValue = (__b ...

  4. Poi中getPhysicalNumberOfCells 与 getLastCellNum的差异

    getPhysicalNumberOfCells 与 getLastCellNum的区别 用org.apache.poi的包做excel导入,无意间发明若是excel文件中有空列,空列后面的数据全部读 ...

  5. Js 操作 Cookies

    <script language=javascript> // cookie其实是一个key=value就是一个cookie而不是 //获得coolie 的值 function cooki ...

  6. Visual Studio 2017 序列号 Key 激活码 VS2017 注册码

    Visual Studio 2017(VS2017) 企业版 Enterprise 注册码 序列号:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(V ...

  7. 《深入理解mybatis原理3》 Mybatis数据源与连接池

    <深入理解mybatis原理> Mybatis数据源与连接池 对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构 ...

  8. js中的数据类型和判断数据类型

    js中的数据类型和判断数据类型 基本数据类型,六大基本数据类型:字符串(String).数字(Number).布尔(Boolean).对象(Object).空(Null).未定义(Undefined) ...

  9. VMware虚拟机创建安装之后不出现VMnet1和VMnet8虚拟网卡

    大家可能遇到过安装虚拟机之后,不出现这两张虚拟网卡,造成一系列的网络问题 VMware虚拟机无法将网络改为桥接状态 本人亲试可行的解决办法 首先把你之前安装的VMware虚拟机卸载,清理得一干二净: ...

  10. python使用tkinter做界面之颜色

    python使用tkinter做界面之颜色       from tkinter import *colors = '''#FFB6C1 LightPink 浅粉红#FFC0CB Pink 粉红#DC ...