推送机制

一,NSNotifcationCenter 

观察者模式。KVO。

NSNotification并不能实现IPC,因为NSNotificationCenter只允许同一个程序中的不同对象进行通信,他们不能跨越不同的应用。

Nofitication对象代表Poster与Observer之间的信息载体,该对象包含如下只读属性;

name:代表该通知的名称;

object:代表该通知的Poster

userInfo:

1,注册

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

//第一个参数为监听者监听object,第二个参数:监听方法,第三个参数:通知名称;

如果object为空,则用于监听任何对象发出的通知;

[NSNotificationCenter defaultCenter] 获取系统默认的通知中心对象;

2)addObseverForName:object:queue:usingBlock: 指定代码块为监听者;

3)removeObserver:

4)removeObserver:name:object: 取消监听者队指定Poster、指定通知的监听;

2,发送消息

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

2),postNotification:需要一个NSNotification对象作为通知

postNotificationName:object://第一个参数:通知名称;第二个:通知的Poster

postNotificationName:object:userInfo: //第三个参数:是NSDicationary对象,用于携带通知的附加信息;

 

 

二,iOS本地通知

1,推送通知的基本目的都是让应用程序能够通知用户某些事情,而且不需要应用程序在前台运行;

区别在于:本地通知由本应用负责调用,只能从当前设备上ios发出;

远程推送通知由远程服务器上的程序发送至App Push Notification serve(APNs),再由APNs把消息推送至设备上对应的程序;

2,本地通知是一个UILocalNotification对象,它有如下属性:

fireDate:指定通知在什么时候触发;

repeatInterval:指定本地通知重复发送的时间间隔;

alertBody:设置本地通知的消息体;

alertAction:设置当设备处于锁屏状态时,显示通知的警告框下方的title

hasAction:设置是否显示Action

alertLaunchImage:当用户通过该通知启动对应的应用时,该属性设置为加载图片

applicationIconBadgeNumber:设置显示在应用程序上红色徽标中的数字;

soundName:设置通知的声音

userInfo:设置该通知携带的附加信息;

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];

3,发送通知: 

scheduleLocalNotification:该方法指定调度通知。通知将会于fireDate指定的时间出发,而且会按repeatInterval指定的时间间隔重复触发;

presentLocalNotificationNow:立即放松通知,该方法会忽略UILocalNotification的fireData属性;

每个应用程序最多只能发送最近(新)的64个本地通知,超过该限制的通知将被操作系统自动放弃。重复的通知会被认为是一个通知;

如果系统发出通知时,应用程序处于前台,系统将触发应用程序委托类的application:didReceiveLocalNotification:方法;

4,在ios应用中,发送本读通知本地通知的步骤:

1),创建UILocalNotification对象;

2),设置UILocalNotification属性;

3),调用UIApplication的方法发送或调用通知;

4),如果希望应用程序在前台运行时可以对通知进行相应的处理,则需要重写应用程序委托类的application:didReceiveLocalNotification:方法;

 

三,iOS远程推送通知

 

UIApplication 提供le如下方法来注册远程Push通知。

-registerForRemoteNotificationTypes:注册远程推送通知;

-unregisterForRemoteNotifications:取消注册远程推送通知;

UIApplicationDelegate中则定义了如下与远程推送通知相关的方法:

-application:didReceiveRemoteNotification:应用程序收到远程Push通知时自动执行该方法;

-application:didRegisterForRemoteNotificationsWithDeviceToken:

-application:didFailToRegisterForRemoteNotificationWithError:

 

posted @ 2015-07-07 11:32  尘恍若梦  阅读(557)  评论(0编辑  收藏  举报