ios推送:本地通知UILocalNotification
2014-09-05 20:22
465 查看
转载自:http://www.2cto.com/kf/201403/285612.html
在去年做过一个小App,其中使用的关键功能就是向用户发送本地通知,可惜当时没有写博客的习惯,所以没有将对应的知识记录下来。最近又遇到了该功能的使用,这一次果断写个博客做下有关UILocalNotification的笔记。
首先是添加一个本地通知到系统中,代码如下:
?
上面的alertBody是设备收到本地通知时横额或锁屏时的主要文字内容,alertActions是锁屏时显示的slide to后面的文字内容。例如:
![](http://www.2cto.com/uploadfile/Collfiles/20140315/2014031509315153.png)
![](http://www.2cto.com/uploadfile/Collfiles/20140315/2014031509315356.png)
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
其取值主要有:
?
分别表示一个世纪、一年、一个月等等,0表示不重复。具体可以查看CFCalendar Reference
repeatInterval的下限应该是NSCalendarUnitMinute,即每分钟重复发送一次通知。
如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。
这里比较不好的一点是该值不能自定义(很遗憾,NSCalendarUnit是个枚举类型),例如你不能塞个10.0给它从而希望它每十秒重复一次。所以如果你想每20分钟发送一次通知,一小时内发送3次,那么只能同时设定三个通知了。
上面的代码运行后,5秒钟之后就可以收到一个本地通知。
在收到通知后,调用程序委托中的下列方法处理:
?
注意这个方法只有在程序启动之后才会执行,因此当程序处于后台时,该方法不会执行。
有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)。例如,我们在viewDidLoad方法中启动添加本地通知的方法,多跑几次,然后把程序在模拟器中删除,再重新跑,并用下列方法输出所有的本地通知:
?
控制台输出:
?
可以看到之前发送的本地通知一直滞留在系统中。
不只是模拟器,在iOS设备上也是这样,博主之前的App在设备上重装时以前的本地通知会继续发送。
因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。
取消方法分为两种。
第一种比较暴力,直接取消所有的本地通知:
?
这个适合在App重装时第一次启动的时候,或还原程序默认设置等场合下使用。
第二种方法是针对某个特定通知的:
?
这时就需要通知有一个标识,这样我们才能定位是哪一个通知。可以在notification的userInfo(一个字典)中指定。
例如:
?
最后建议本地通知不要发得太频繁,不然用户会觉得非常的烦
转载自:http://blog.csdn.net/l_ch_g/article/details/8767402
第一步:创建本地推送
// 创建一个本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//设置10秒之后
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
// 设置推送时间
notification.fireDate = pushDate;
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
notification.alertBody = @"推送内容";
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
notification.userInfo = info;
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
// 图标上的数字减1
application.applicationIconBadgeNumber -= 1;
}
第三步:解除本地推送
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"对应的key值"]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//判断是否找到已经存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}
在去年做过一个小App,其中使用的关键功能就是向用户发送本地通知,可惜当时没有写博客的习惯,所以没有将对应的知识记录下来。最近又遇到了该功能的使用,这一次果断写个博客做下有关UILocalNotification的笔记。
首先是添加一个本地通知到系统中,代码如下:
?
![](http://www.2cto.com/uploadfile/Collfiles/20140315/2014031509315153.png)
![](http://www.2cto.com/uploadfile/Collfiles/20140315/2014031509315356.png)
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
其取值主要有:
?
repeatInterval的下限应该是NSCalendarUnitMinute,即每分钟重复发送一次通知。
如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。
这里比较不好的一点是该值不能自定义(很遗憾,NSCalendarUnit是个枚举类型),例如你不能塞个10.0给它从而希望它每十秒重复一次。所以如果你想每20分钟发送一次通知,一小时内发送3次,那么只能同时设定三个通知了。
上面的代码运行后,5秒钟之后就可以收到一个本地通知。
在收到通知后,调用程序委托中的下列方法处理:
?
有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)。例如,我们在viewDidLoad方法中启动添加本地通知的方法,多跑几次,然后把程序在模拟器中删除,再重新跑,并用下列方法输出所有的本地通知:
?
?
不只是模拟器,在iOS设备上也是这样,博主之前的App在设备上重装时以前的本地通知会继续发送。
因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。
取消方法分为两种。
第一种比较暴力,直接取消所有的本地通知:
?
第二种方法是针对某个特定通知的:
?
例如:
?
转载自:http://blog.csdn.net/l_ch_g/article/details/8767402
第一步:创建本地推送
// 创建一个本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//设置10秒之后
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
// 设置推送时间
notification.fireDate = pushDate;
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
notification.alertBody = @"推送内容";
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
notification.userInfo = info;
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
// 图标上的数字减1
application.applicationIconBadgeNumber -= 1;
}
第三步:解除本地推送
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"对应的key值"]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//判断是否找到已经存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}
相关文章推荐
- iOS推送之本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- IOS本地推送通知的一些属性 UILocalNotification
- iOS本地推送通知UILocalNotification(可以用做类似闹钟提醒)
- ios推送:本地通知UILocalNotification
- iOS --- 本地推送通知 UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS推送 之 本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios本地通知UILocalNotification以及区分谁触发了通知
- 【iOS开发-119】ipa打包、单元测试test、本地通知UILocalNotification
- iOS 浅谈本地通知 UILocalNotification
- iOS学习笔记45—本地通知UILocalNotification
- iOS本地通知UILocalNotification