所有的内容融为一体,去除某一个项不知道结果如何。

最主要的前提:APP 会长期保留在后台

1.在info.plist 文件里面,加入 audio 后台请求

2.当APP 点击home进入后台之后,请求一个background handler,当handler快到期的时候,播放一个空mp3,再请求一个新的handler(不知道这样是否合适)

3. APP 分为“Alive”状态下

3.1 点击”通知“进入,会依次启动  didReceiveLocalNotification跟  applicationWillEnterForeground函数。其中applicationWillEnterForeground函数在第一次APP启动的时候是不会进入的。

3.2 点击”ICON“ 进入,会启动 applicationWillEnterForeground 函数,并不会启动didReceiveLocalNotification函数。

4.APP被结束后台状态下

4.1 点击”通知“进入,会启动  didFinishLaunchingWithOptions函数,并且有通知进入。其中applicationWillEnterForeground函数不会进入(3.1讲过了)

4.2 点击”ICON“ 进入,会启动 didFinishLaunchingWithOptions 函数,但是没有通知进入。

相关代码:

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

{

/*

{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"dddddd" message:notification.soundName delegate:nil cancelButtonTitle:@"" otherButtonTitles:@"", nil];

[alert show];

}

*/

NSLog(@"收到通知-开启闹钟");

[AlarmOpt setAlarmFromNoti];

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleNotifiction:) userInfo:notification repeats:NO];

}

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

// Override point for customization after application launch

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

mainVC = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];

self.window.rootViewController = mainVC;

{

//VoiceViewController * vc = [[VoiceViewController alloc]initWithNibName:@"VoiceViewController" bundle:nil];

//NumViewController * vc = [[NumViewController alloc]initWithNibName:@"NumViewController" bundle:nil];

//BIHuaViewController * vc = [[BIHuaViewController alloc]initWithNibName:@"BIHuaViewController" bundle:nil];

//self.window.rootViewController = vc;

//[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(gotoNumViewB) userInfo:nil repeats:NO];

}

//

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])

{

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

}

UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if( !localNotif )

{

/*

{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"ggggggg" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:@"", nil];

[alert show];

}

*/

UILocalNotification * curNot = [AlarmOpt getCurrentNot];

if(curNot )

{

NSLog(@"进入前台-开启闹钟");

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleNotifiction:) userInfo:curNot repeats:NO];

}

else

{

[AlarmOpt startAlarm];

//

[RFRateMe showRateAlertAfterTimesOpened:5];

}

}

else

{

/*

{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"hhhhhhh" message:localNotif.soundName delegate:nil cancelButtonTitle:@"" otherButtonTitles:@"", nil];

[alert show];

}

*/

NSLog(@"开机启动-开启闹钟");

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(handleNotifiction:) userInfo:localNotif repeats:NO];

}

//

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

[[AVAudioSession sharedInstance] setActive: YES error: nil];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

//

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

[tickTimer invalidate];

tickTimer = nil;

tickTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(tick) userInfo:nil repeats:YES];

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void){

NSLog(@"over 1");

}];

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

NSLog(@"进入前台");

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(delayEnterFor) userInfo:nil repeats:NO];

}

-(void)delayEnterFor

{

UILocalNotification * curNot = [AlarmOpt getCurrentNot];

if(curNot )

{

NSLog(@"进入前台-开启闹钟");

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleNotifiction:) userInfo:curNot repeats:NO];

}

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

-(void)gotoShakeView:(UILocalNotification*)noti

{

//ShakeViewController * shakeVC = nil;

shakeVC = nil;

shakeVC = [[ShakeViewController alloc]initWithNibName:@"ShakeViewController" bundle:nil];

shakeVC.locationNoti = noti;

shakeVC.view.frame = [UIScreen mainScreen].bounds;

[self.window.rootViewController.view addSubview:shakeVC.view];

}

-(void)gotoNumView:(UILocalNotification*)noti

{

//NumViewController * numVC = nil;

numVC = nil;

numVC = [[NumViewController alloc]initWithNibName:@"NumViewController" bundle:nil];

numVC.locationNoti = noti;

numVC.view.frame = [UIScreen mainScreen].bounds;

[self.window.rootViewController.view addSubview:numVC.view];

}

-(void)gotoBiHuaView:(UILocalNotification*)noti

{

//BIHuaViewController * biVC = nil;

biVC = nil;

biVC = [[BIHuaViewController alloc]initWithNibName:@"BIHuaViewController" bundle:nil];

biVC.locationNoti = noti;

biVC.view.frame = [UIScreen mainScreen].bounds;

[self.window.rootViewController.view addSubview:biVC.view];

}

-(void)gotoVoiceView:(UILocalNotification*)noti

{

//VoiceViewController * voiVC = nil;

voiVC = nil;

voiVC = [[VoiceViewController alloc]initWithNibName:@"VoiceViewController" bundle:nil];

voiVC.locationNoti = noti;

voiVC.view.frame = [UIScreen mainScreen].bounds;

[self.window.rootViewController.view addSubview:voiVC.view];

}

-(void)handleNotifiction:(NSTimer*)timer

{

UILocalNotification * noti = [timer userInfo];

if( !noti  || _bInCloseAlarm )

{

return;

}

NSData * data = [noti.userInfo objectForKey:ALARM_INFO_KEY];

AlarmInfo * info  = [NSKeyedUnarchiver unarchiveObjectWithData:data];

if( info )

{

_bInCloseAlarm = YES;

NSLog(@"======接到通知了=========");

if( [info.stopType isEqualToString:ALARM_STOP_TYPE_SHAKE])

{

[self gotoShakeView:noti];

}

else if( [info.stopType isEqualToString:ALARM_STOP_TYPE_NUM])

{

[self gotoNumView:noti];

}

else if( [info.stopType isEqualToString:ALARM_STOP_TYPE_BIHUA])

{

[self gotoBiHuaView:noti];

}

else if( [info.stopType isEqualToString:ALARM_STOP_TYPE_BLOW])

{

[self gotoVoiceView:noti];

}

}

}

- (void)tick

{

NSLog(@"tick:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);

//

if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 61.0)

{

//

if( !audioPlayer )

{

NSError *error = nil;

NSString * str = [[NSBundle mainBundle]pathForResource:@"empty" ofType:@"mp3"];

NSURL * url = [NSURL fileURLWithPath:str];

audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];

[audioPlayer prepareToPlay] ;

}

[audioPlayer play];

[NSThread sleepForTimeInterval:1];

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void){

NSLog(@"over 2");

}];

}

}

IOS 通知 alarm 记录的更多相关文章

  1. iOS 通知观察者的被调函数不一定运行在主线程

    Tony in iOS | 08/08/2013 iOS 通知观察者的被调函数不一定运行在主线程 今天修复Bug时候发现的一个小细节,记录下. 问题描述 事情是这样的:我在A视图(UITableVie ...

  2. iOS通知的整理笔记

    iOS通知用于高耦合界面的传值确实方便快捷. 需要实现模态弹出的视图控制器上,有一个视图控制器可以导航.这必定要将这个视图控制器的导航视图控制器naVC.view添加到模态弹出的视图控制器presen ...

  3. iOS开发之记录用户登录状态

    iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...

  4. iOS 通知的变化ios9-10,新功能展示

    二.新功能展示 1  使用 /iOS通知新功能玩法 2.  全面   iOS10里的通知与推送详情 一.变化 四.Notification(通知) 自从Notification被引入之后,苹果就不断的 ...

  5. IOS开发之记录用户登陆状态,ios开发用户登陆

    IOS开发之记录用户登陆状态,ios开发用户登陆 上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreDa ...

  6. aop 例外通知就是记录业务方法出现错误 并保存到日志里面的功能

    aop 例外通知就是记录业务方法出现错误 并保存到日志里面的功能

  7. iOS通知中心

    iOS通知中心 它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信. 当通知中心接受到消息后会 ...

  8. delphi IOS 通知 TNotification

    delphi  IOS 通知 TNotification http://blogs.embarcadero.com/ao/2013/05/01/39450 TNotification http://d ...

  9. iOS 通知扩展插件

    iOS 通知扩展插件 目录 iOS 通知扩展插件 Notification Service Extension 新建一个target 代码实现 注意事项 UINotificationConentExt ...

随机推荐

  1. Oracle 6 - 锁和闩 - 并发问题和隔离级别

    并发带来的问题 1.脏读dirty read 脏读的问题是transaction读到了没有被提交的数据.例如,T1更新了data1,还没提交,这时T2读取了更新后的data1, 用于计算和更新别的值, ...

  2. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  3. lintcode:最大子正方形

    题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  4. [itint5]最短路径遍历点

    http://www.itint5.com/oj/#50 此题有点难,参考了这篇文章,是个两条路的DP: http://blog.csdn.net/a83610312/article/details/ ...

  5. PHP中的抽象类与接口

    抽象类 php5支持抽象类和抽象方法.类前加 abstract, 此类就成为抽象类,无法被实例化,此类天生就是用来被继承的,给子类提供了一个类的模板; 类方法前加 abstract,是抽象方法,抽象方 ...

  6. 【Linux高频命令专题(8)】五大查询命令

    find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...

  7. SpringMVC学习总结(六)——SpringMVC文件上传例子(2)

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...

  8. HTML5入门4---HTML5 与 HTML4 同一网页的不同写法

    HTML4写法 css: body { font-family: "Lucida Sans Unicode", "Lucida Grande", Verdana ...

  9. WCF揭秘(一)——简单的WCF开发实例

    一.WCF是什么 WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发.WCF将DCOM.Remoting.Web Service.WSE.MSMQ.AJAX服务 ...

  10. RedHat6配置yum源 (32位)

    由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用, 如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除redhat原有的yum rpm ...