iOS UIKit框架:本地通知 UILocalNotification

程序需要做 聊天消息通知,因为之前没有做过通知,不知道用本地通知还是远程通知,就看了下本地通知

1、先看下添加本地通知代码

    UILocalNotification* noti = [[UILocalNotification alloc]init];
     // 设置通知的信息
    noti.alertBody = @"本地通知";
    noti.alertAction = @"打开程序";
    noti.applicationIconBadgeNumber = 1;
    noti.repeatInterval = NSCalendarUnitMinute;
    noti.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// ios8后,需要添加这个注册,才能得到授权  
  if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
                                                                             categories:nil];  
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  

 


// 发出通知 [[UIApplication sharedApplication] scheduleLocalNotification:noti];

然后运行程序,就可以发出通知了, 但是程序 必须在后台才可以看到通知的效果,而如果应用程序正在运行时,是看不到通知的效果。

 2、移除通知

    // 移除所有的通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

3、监听本地通知的点击

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"%s",__func__);
}

总结下:

  本地通知,只能定时发送些消息,不能满足我的需求,看来是要用远程通知

posted @ 2015-11-18 21:40  gallon  阅读(198)  评论(0编辑  收藏  举报