/**
* 解除某一个本地推送
*
* @param nameStr 本地推送的名字,需要自己起一个,可以用时间戳,同一时间不可能创建多个备忘。
*/
+(void)closeSomeLocalNotificationWithName:(NSString*)timestamp{
if (timestamp==nil) {
return;
}
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//如果存在本地推送数组,则遍历该数组找出符合条件的推送,关掉
if (localArray&&[localArray count]>0) {
for (UILocalNotification *notification in localArray)
{
NSDictionary *dict = notification.userInfo;
if ([timestamp isEqualToString:ClearAllLocalNotification]) {
[app cancelLocalNotification:notification];
}
else if ([dict isKindOfClass:[NSDictionary class]]) {
NSString *inKey = [dict objectForKey:@"name"];
if ([inKey isEqualToString:timestamp]) {
[app cancelLocalNotification:notification];
break;
}
}
}
}
}
/**
* 添加一个本地推送
*
* @param name 推送名称,一般使用时间戳
* @param remind 本地推送发出的时间
* @param repeat 重复类型
*/
+(void)addLocalNotificationWithName:(NSString*)name
remind:(NSDate*)remind
repeat:(NSInteger)repeat
content:(NSString*)content
{
if([[NSDate date] timeIntervalSinceDate:remind]>0 && repeat==0){
//已逾期且不重复提醒,则不注册本地推送
return;
}
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
// //设置通知的提醒时间
// NSDate* remindTime = [FunctionUnit convertDateFromString:remind Formate:TimeStrFormat];
notification.fireDate=remind;//多长时间后后通知
notification.timeZone=[NSTimeZone defaultTimeZone];//使用本地时区
// 设置重复间隔
switch (repeat) {
case 0://不循环
notification.repeatInterval=0;//循环次数,kCFCalendarUnitWeekday一周一次
break;
case 1://每天循环
notification.repeatInterval=kCFCalendarUnitDay;
break;
case 2://每周循环
notification.repeatInterval=kCFCalendarUnitWeek;
break;
case 3://每月循环
notification.repeatInterval=kCFCalendarUnitMonth;
break;
case 4://每季循环
notification.repeatInterval=kCFCalendarUnitQuarter;
break;
case 5://每年循环
notification.repeatInterval=kCFCalendarUnitYear;
break;
default:
break;
}
//设置应用程序右上角的提醒个数
notification.applicationIconBadgeNumber+=1;
//声音,可以换成alarm.soundName = @"myMusic.caf"
notification.soundName= UILocalNotificationDefaultSoundName;
//去掉下面2行就不会弹出提示框
//提示信息 弹出提示框
notification.alertBody=[NSString stringWithFormat:@"您有备忘:%@需要处理",content];
//提示框按钮
notification.alertAction = @"确定";
//是否显示额外的按钮,为no时alertAction消失
notification.hasAction = NO;
//设置userinfo方便在之后需要撤销的时候使用,直接加一个时间戳好了
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",@"memo",@"memo", nil];
//添加额外的信息
notification.userInfo = infoDict;
// 将通知添加到系统中
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}