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

iOS UILocalNotification定时提醒

2015-08-11 21:22 477 查看
UILocalNotification *notifity=[[UILocalNotification
alloc] init];
// NSString *strTimer = @"18:58";
// NSDateFormatter *formattr=[[NSDateFormatter alloc] init];
// //格式化时间
// [formattr setDateFormat:@"HH:mm"];
// //触发通知时间
//
// NSDate *now=[formattr dateFromString:[NSString stringWithFormat:@"%@",strTimer]];
// notifity.fireDate=now;

NSDate *now2=[NSDate
new];
notifity.fireDate=[now2
addTimeInterval:10];

//时区
notifity.timeZone=[NSTimeZone
defaultTimeZone];
//通知重复提示的单位,可以是周(NSWeekdayCalendarUnit)分钟(NSMinuteCalendarUnit)秒(NSSecondCalendarUnit)月(NSMonthCalendarUnit)年(NSYearCalendarUnit)

notifity.repeatInterval=NSCalendarUnitDay;

//通知内容
notifity.alertBody=@"这是一个通知";
//通知触发时播放的声音
notifity.soundName=UILocalNotificationDefaultSoundName;
//如果要在通知栏中携带参数信息,可以使用下面的方式:

NSDictionary *dic = [NSDictionary
dictionaryWithObject:@"name"
forKey:@"key"];

notifity.userInfo = dic;
//如果软件是在运行中,则可以通过AppDelegate中的回调方法获取并处理参数信息:

//执行通知注册

[[UIApplication
sharedApplication] scheduleLocalNotification:notifity];

//
需要注意:iOS8系统变更了注册方法,需要在ApplicationDelegate里面注册通知才可以。
if ([UIApplication
instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
categories:nil]];

}

// 可以通过两种方式取消注册的本地通知,一种是取消指定的通知,第二种是取消所有的注册通知:

// [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
// [[UIApplication sharedApplication] cancelAllLocalNotifications];

转载地址:http://www.th7.cn/Program/IOS/201407/232425.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: