1.各种通知


在iOS中通知主要有:广播通知(broadcast notification)、本地通知(local notification)和推送通知(push notification)即远程通知。


广播通知是Cocoa Touch框架中实现观察者模式的一种机制,它可以在一个应用内部的多个对象之间发送消息。


本地通知和推送通知(远程通知)是给用户的一种提示,它的提示方式有警告对话框、发出声音、振动、和在应用图标上显示数字等等。




2.广播通知(Board Notification)


通知机制是“一对多”的对象之间的通信。在通知机制中对某个通知感兴趣的所有对象都可以成为接受者。


首先,这些将要接受通知的对象需要向通知中心(NSNotificationCenter)发出addObserver:selector:name:object:消息进行注册,当投送对象投送通知给NotificationCenter时,通知中心就会把通知广播给注册过的接受者。但是所有的接受者都不知道通知是谁投送的,不会关心它的细节。投送对象与接受者是一对多的关系。


接受者如果对该通知不在关注,可以给通知中心发出removeObserver:name:object:消息接触注册,以后不在接受该通知。


这里我们以APP将要终止时发出通知为例:


投递通知:


- (void)applicationWillTerminate:(UIApplication *)application
{
    NSDate *date = [NSDate date];
    //创建userInfo对象,在发送通知时传递给通知接受者
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:date
                                                         forKey:@"TerminateDate"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppWillTerminateNotification"    //通知名称
                                                        object:self                               //投送通知时传递过来的对象
                                                      userInfo:dataDict];                         //投送通知时定义的字典对象,可用于传递数据
}


   接受通知:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self                               //将self指定为通知观察者
                                             selector:@selector(handleTerminate:)        //接受到通知后执行的方法
                                                 name:@"AppWillTerminateNotification"    //通知的名称
                                               object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];                          //self对象销毁时,移除通知
}


#pragma mark - 接受通知后的处理方法
-(void)handleTerminate:(NSNotification*)notification
{
    NSDictionary *theData = [notification userInfo];

    if (theData != nil) {
        NSDate *date = [theData objectForKey:@"TerminateDate"];
        NSLog(@"MainViewController App Terminate Date: %@", date);
    }
}



3.本地通知(LocalNotification)




ios发通知 苹果通知消息_通知中心




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    
    
    
    //本地通知
    UILocalNotification *notification=[[UILocalNotification alloc] init];
    if (notification!=nil) {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        
        NSDate *now=[NSDate new];
        notification.fireDate=[now dateByAddingTimeInterval:10]; //触发通知的时间
        notification.repeatInterval = kCFCalendarUnitMinute;     //循环次数,kCFCalendarUnitWeekday一周一次
        
        notification.timeZone=[NSTimeZone defaultTimeZone];
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.alertBody=@"该去吃晚饭了!";               //若alertBody不为nil,则显示alert
        
        //在模拟器上暂时还没有测试出以下两个属性的作用
        notification.alertAction = @"打开";  //显示alert右侧提示按钮
        notification.hasAction = YES;       //默认为YES,是否显示额外的按钮,为no时alertAction消失
        
        notification.applicationIconBadgeNumber = 1; //设置app图标右上角的数字
        
        //下面设置本地通知发送的消息,这个消息可以接受
        NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
        notification.userInfo = infoDic;
        //发送通知
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    
    
    [self.window makeKeyAndVisible];
    return YES;
}
/**
 *  该APP在foremost并可见时,系统发送本地通知,本地通知不会显示警告、不会标记APP icon、不会播放音乐。
 *  此时若实现了以下方法,便转而执行以下方法,读取notification.userInfo的内容。
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
    
    //该方法是在发生了本地通知后,进行的处理实现 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification"
                                                    message:notification.alertBody
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
    
    NSDictionary* dic = [[NSDictionary alloc]init];
    //这里可以接受到本地通知中心发送的消息
    dic = notification.userInfo;
    NSLog(@"user info = %@",[dic objectForKey:@"key"]);
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    //当APP编程active时将badgeNumber变成0。
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}



ios发通知 苹果通知消息_接受者_02




4.远程推送(push Notification /remote Notification)



推送通知链接: 点击打开链接