您的位置:首页 > 产品设计 > UI/UE

UILocalNotification 本地通知

2014-03-20 11:16 411 查看
// 初始化本地通知对象
UILocalNotification *notification = [[UILocalNotification alloc] init];

//  设置通知提醒时间(退出前台进入后台后开始算时间)
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

//  设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;

//  设置提醒文字的内容
notification.alertBody = @"马总,你该吃药了";
notification.alertAction = @"好的";//  锁屏的时候显示
notification.hasAction = YES;

//  提示音效(真机才有用)
notification.soundName = @"管钟琴.caf";
notification.alertLaunchImage = @"loginback@2x.png";

//  设置应用程序右上角的提醒个数
notification.applicationIconBadgeNumber = 5;

//  设定通知的userInfo,用来标识该通知(方便指定移除通知)
NSDictionary *aUserInfo = [[NSDictionary alloc] init];
aUserInfo = @{@"key": @"value"};
notification.userInfo = aUserInfo;

//  将通知添加到系统中
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//  在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification:
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

//  repeatInterval表示通知的重复间隔,在SDK中定义如下:
1. NSCalendarUnitEra                = kCFCalendarUnitEra,
2. NSCalendarUnitYear               = kCFCalendarUnitYear,
3. NSCalendarUnitMonth              = kCFCalendarUnitMonth,
4. NSCalendarUnitDay                = kCFCalendarUnitDay,
5. NSCalendarUnitHour               = kCFCalendarUnitHour,
6. NSCalendarUnitMinute             = kCFCalendarUnitMinute,
7. NSCalendarUnitSecond             = kCFCalendarUnitSecond,
8. NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
9. NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,

repeatInterval的下限应该是NSCalendarUnitMinute,即每分钟重复发送一次通知。
如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。
例如你不能塞个10.0给它从而希望它每十秒重复一次。所以如果你想每20分钟发送一次通知,一小时内发送3次,那么只能同时设定三个通知了。

//  点击通知从后台进入时响应的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

//  改变应用右上角提示数
[application setApplicationIconBadgeNumber:0];

}
一般需要应用程序后台运行时才会显示提示,前台运行时一般不显示提示。如果想要当应用程序前台应行时也显示提示,则可以通过将下面函数加到appDelegate中实现:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

UIApplicationState state = application.applicationState;
//    NSLog(@"%@,%d",notification,state);
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:@"OK",nil];
[alert show];
[alert release];
}
}

有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在
没有从系统中移除.不只是模拟器,在iOS设备上也是这样,我之前的App在设备上重装时以前的本地通知会继续发送.)
因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。
取消方法分为两种。
1.取消所有的本地通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];

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

2.是针对某个特定通知的:

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

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

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

///本地移除
NSLog(@"%s",__FUNCTION__);
//获取当前应用所有的通知
NSArray* localNotifications=[[UIApplication sharedApplication] scheduledLocalNotifications];

if (localNotifications) {

for (UILocalNotification* notification in localNotifications) {

NSDictionary* dic=notification.userInfo;

if (dic) {
NSString* value=[dic objectForKey:@"key"];
if ([value isEqualToString:@"value"]) {
//取消推送 (指定一个取消)
[app cancelLocalNotification:notification];

break;
}
}

}
}

//取消当前应用所有的推送
//[app cancelAllLocalNotifications];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UI