iOS中添加/删除本地通知(闹钟)的方法

iOS系统默认可以向程序中增加最多64个本地通知,首先要判断机器是不是支持本地通知,然后在为每一个通知指定一个标识,用于取消时。  
//添加本地通知的方法
/*
 message:显示的内容
 firedate:闹钟的时间
 alarmKey:闹钟的ID
 */
+(void)addLocalNotificationWithMessage:(NSString *)message
 FireDate:(NSDate *) fireDate
 AlarmKey:(NSString *)alarmKey
{
 UILocalNotification *notification=[[UILocalNotification alloc] init];
 if (notification!=nil) {
 
 notification.fireDate=fireDate;
 
 notification.timeZone=[NSTimeZone defaultTimeZone];
 notification.soundName= UILocalNotificationDefaultSoundName;
 
 notification.alertBody=message;
 notification.hasAction = NO;
 notification.userInfo=[[NSDictionary alloc] initWithObjectsAndKeys:alarmKey,@"AlarmKey", nil];
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
 }
 [notification release];
}
 
/*
 删除本地通知
 */
+(void)deleteLocalNotification:(NSString *) alarmKey
{
 NSArray * allLocalNotification=[[UIApplication sharedApplication] scheduledLocalNotifications];
 
 for (UILocalNotification * localNotification in allLocalNotification) {
 NSString * alarmValue=[localNotification.userInfo objectForKey:@"AlarmKey"];
 if ([alarmKey isEqualToString:alarmValue]) {
 [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
 }
 }
}

转载于:https://www.cnblogs.com/liuxingzi/archive/2012/11/22/3404277.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值