/**
* 解除某一個本地推送
*
* @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];
}
}