iOS 本地通知详解

//
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     
     _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
     
     _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
     
     //设置全局导航条风格和颜色
     [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
     [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
     
     KCMainViewController *mainController=[[KCMainViewController alloc]init];
     _window.rootViewController=mainController;
     
     [_window makeKeyAndVisible];
     //如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
     if  ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
         [self addLocalNotification];
     } else {
         [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];
     }
     
     return  YES;
}
#pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行)
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
     if  (notificationSettings.types!=UIUserNotificationTypeNone) {
         [self addLocalNotification];
     }
}
#pragma mark 进入前台后设置消息信息
-(void)applicationWillEnterForeground:(UIApplication *)application{
     [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0]; //进入前台取消应用消息图标
}
#pragma mark - 私有方法
#pragma mark 添加本地通知
-(void)addLocalNotification{
     
     //定义本地通知对象
     UILocalNotification *notification=[[UILocalNotification alloc]init];
     //设置调用时间
     notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0]; //通知触发的时间,10s以后
     notification.repeatInterval=2; //通知重复次数
     //notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
     
     //设置通知属性
     notification.alertBody=@ "最近添加了诸多有趣的特性,是否立即体验?" //通知主体
     notification.applicationIconBadgeNumber=1; //应用程序图标右上角显示的消息数
     notification.alertAction=@ "打开应用" //待机界面的滑动动作提示
     notification.alertLaunchImage=@ "Default" ; //通过点击通知打开应用时的启动图片,这里使用程序启动图片
     //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音
     notification.soundName=@ "msg.caf" ; //通知声音(需要真机才能听到声音)
     
     //设置用户信息
     notification.userInfo=@{@ "id" :@1,@ "user" :@ "Kenshin Cui" }; //绑定到通知上的其他附加信息
     
     //调用通知
     [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#pragma mark 移除本地通知,在不需要此通知时记得移除
-(void)removeNotification{
     [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end

首先是添加一个本地通知到系统中,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 初始化本地通知对象
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
     // 设置通知的提醒时间
     NSDate *currentDate   = [NSDate date];
     notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区
     notification.fireDate = [currentDate dateByAddingTimeInterval: 5.0 ];
     
     // 设置重复间隔
     notification.repeatInterval = kCFCalendarUnitDay;
     
     // 设置提醒的文字内容
     notification.alertBody   = @ "Wake up, man" ;
     notification.alertAction = NSLocalizedString(@ "起床了" , nil);
     
     // 通知提示音 使用默认的
     notification.soundName= UILocalNotificationDefaultSoundName;
     
     // 设置应用程序右上角的提醒个数
     notification.applicationIconBadgeNumber++;
     
     // 设定通知的userInfo,用来标识该通知
     NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init];
     aUserInfo[kLocalNotificationID] = @ "LocalNotificationID" ;
     notification.userInfo = aUserInfo;
     
     // 将通知添加到系统中
     [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

上面的alertBody是设备收到本地通知时横额或锁屏时的主要文字内容,alertActions是锁屏时显示的slide to后面的文字内容。例如:

\\


repeatInterval表示通知的重复间隔,在SDK中定义如下:

<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">@property(nonatomic) NSCalendarUnit repeatInterval; // 0 means don't repeat
其取值主要有:

?
1
2
3
4
5
6
7
8
9
NSCalendarUnitEra                = kCFCalendarUnitEra,
NSCalendarUnitYear               = kCFCalendarUnitYear,
NSCalendarUnitMonth              = kCFCalendarUnitMonth,
NSCalendarUnitDay                = kCFCalendarUnitDay,
NSCalendarUnitHour               = kCFCalendarUnitHour,
NSCalendarUnitMinute             = kCFCalendarUnitMinute,
NSCalendarUnitSecond             = kCFCalendarUnitSecond,
NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,

分别表示一个世纪、一年、一个月等等,0表示不重复。具体可以查看CFCalendar Reference

repeatInterval的下限应该是NSCalendarUnitMinute,即每分钟重复发送一次通知。

如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。

这里比较不好的一点是该值不能自定义(很遗憾,NSCalendarUnit是个枚举类型),例如你不能塞个10.0给它从而希望它每十秒重复一次。所以如果你想每20分钟发送一次通知,一小时内发送3次,那么只能同时设定三个通知了。



上面的代码运行后,5秒钟之后就可以收到一个本地通知。

在收到通知后,调用程序委托中的下列方法处理:

?
1
2
3
4
5
6
-( void )application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
     NSLog(@ "Application did receive local notifications" );
     
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@ "Hello" message:@ "welcome" delegate:nil cancelButtonTitle:@ "OK" otherButtonTitles:nil, nil];
     [alert show];
}

注意这个方法只有在程序启动之后才会执行,因此当程序处于后台时,该方法不会执行。


有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)。例如,我们在viewDidLoad方法中启动添加本地通知的方法,多跑几次,然后把程序在模拟器中删除,再重新跑,并用下列方法输出所有的本地通知:

?
1
2
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(@ "%@" , localNotifications);

控制台输出:

?
1
2
3
4
5
6
7
8
2014 - 03 - 14 15 : 46 : 37.145 LocalNotificationDemo[ 4419 :60b] (
     "<uiconcretelocalnotification: 0xa32ce30=" ">{fire date = Friday, March 14, 2014 at 3:38:16 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:38:16 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}" ,
     "<uiconcretelocalnotification: 0xa32dfc0=" ">{fire date = Friday, March 14, 2014 at 3:44:45 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:44:45 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}" ,
     "<uiconcretelocalnotification: 0xa32e470=" ">{fire date = Friday, March 14, 2014 at 3:44:55 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:44:55 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}" ,
     "<uiconcretelocalnotification: 0xa32e950=" ">{fire date = Friday, March 14, 2014 at 3:45:13 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:45:13 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}" ,
     "<uiconcretelocalnotification: 0xa32edb0=" ">{fire date = Friday, March 14, 2014 at 3:45:29 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:45:29 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}" ,
     "<uiconcretelocalnotification: 0xa32e870=" ">{fire date = Friday, March 14, 2014 at 3:46:28 PM China Standard Time, time zone = Asia/Chongqing (GMT+8) offset 28800, repeat interval = NSDayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, March 15, 2014 at 3:46:28 PM China Standard Time, user info = {\n    ClockID = LocalNotificationID;\n}}"
)</uiconcretelocalnotification:></uiconcretelocalnotification:></uiconcretelocalnotification:></uiconcretelocalnotification:></uiconcretelocalnotification:></uiconcretelocalnotification:>

可以看到之前发送的本地通知一直滞留在系统中。

不只是模拟器,在iOS设备上也是这样,博主之前的App在设备上重装时以前的本地通知会继续发送。

因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。

取消方法分为两种。

第一种比较暴力,直接取消所有的本地通知:

?
1
[[UIApplication sharedApplication] cancelAllLocalNotifications];

这个适合在App重装时第一次启动的时候,或还原程序默认设置等场合下使用。

第二种方法是针对某个特定通知的:

?
1
- ( void )cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);

这时就需要通知有一个标识,这样我们才能定位是哪一个通知。可以在notification的userInfo(一个字典)中指定。

例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-( void )application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
     NSLog(@ "Application did receive local notifications" );
     
     // 取消某个特定的本地通知
     for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
         NSString *notiID = noti.userInfo[kLocalNotificationID];
         NSString *receiveNotiID = notification.userInfo[kLocalNotificationID];
         if ([notiID isEqualToString:receiveNotiID]) {
             [[UIApplication sharedApplication] cancelLocalNotification:notification];
             return ;
         }
     }
     
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@ "Hello" message:@ "welcome" delegate:nil cancelButtonTitle:@ "OK" otherButtonTitles:nil, nil];
     [alert show];
}


最后建议本地通知不要发得太频繁,不然用户会觉得非常的烦。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值