ios通知机制

  ios通知机制是ios消息传递机制的一部分,关于ios消息传递机制有篇不错的博文,http://www.cocoachina.com/applenews/devnews/2013/1216/7543.html
[/size]
   IOS中还有一类产生事件的功能,那就是通知。通过通知,可以在一定的条件下触发响应的事件。类似于Android中的广播机制(Broadcase Receiver),接收到通知(广播)后,便可执行指定的方法。

  通过NSNotificationCenter获取通知对象,注册并使用通知。

   
  在iOS下应用分为两种不同的Notification种类,本地和远程。
  1.本地UILocalNotificaiton的使用
 
  本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

  本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的 属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。


  Demo:
 
//发送通知

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

    if (notification!=nil) { 

        NSDate *now=[NSDate new]; 

        notification.fireDate=[now dateByAddingTimeInterval:10];//10秒后通知

        notification.repeatInterval=0;//循环次数,kCFCalendarUnitWeekday一周一次

        notification.timeZone=[NSTimeZone defaultTimeZone];

        notification.applicationIconBadgeNumber=1; //应用的红色数字 

        notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf" 

        //去掉下面2行就不会弹出提示框

         notification.alertBody=@"通知内容";//提示信息 弹出提示框

         notification.alertAction = @"打开";  //提示框按钮 

        //notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失



       // NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];

        //notification.userInfo = infoDict; //添加额外的信息

        

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];      

    }

    [notification release];


   取消通知
  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    application.applicationIconBadgeNumber = 0;
    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    //点击提示框的打开
    application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //当程序还在后天运行
    application.applicationIconBadgeNumber = 0;
}


  登记UILocalNotification

在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。登记UILocalNotification * localNotification的方式为:

   [[UIApplication sharedApplication]  scheduleLocalNotification:localNotification];

在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification:

   [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

  处理UILocalNotification

在提醒框动作按钮被点击后,应用开始运行时,可以在-(BOOL)application:didFinishLaunchingWithOptions:这个Application delegate方法中处理。可以通过以下方式来加载为最近未处理的Notification:

   UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

如果应用正在运行时,可以通过覆盖在Application Delegate中的方法-(void)application:didReceiveLocalNotification:来处理Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。
取消UILocalNotification

可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消

   [[UIApplication sharedApplication] cancelLocalNotification:localNotification];

   [[UIApplication sharedApplication] cancelAllLocalNotification];


   2.NSNotificationCenter消息通信机制介绍
   ---是专门供程序中不同类间的消息通信而设置的

   注册通知:即要在什么地方接受消息

               [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];

      参数介绍:

          addObserver: 观察者,即在什么地方接收通知;

        selector: 收到通知后调用何种方法;

        name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

   发送通知:
   调用观察者处的方法。

           [[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];

          参数:

         postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

                 object:传递的参数

   注册方法的写法:

- (void) mytest:(NSNotification*) notification

{

   id obj = [notification object];//获取到传递的对象

}


 
附:注册键盘升启关闭消息

    //键盘升起 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //键盘降下
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

  
   远程通知:http://www.cocoachina.com/newbie/tutorial/2012/0104/3827.html





























猜你喜欢

转载自pupin9.iteye.com/blog/2079081
今日推荐