// 添加本地通知
-(void)addLocalNotificationWithTime:(NSString *)time{
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *now = [formatterdateFromString:time];//触发通知的时间
UILocalNotification *noti = [[UILocalNotificationalloc] init];
if (noti) {
//设置推送时间
noti.fireDate = now;//=now
//设置时区
noti.timeZone = [NSTimeZonedefaultTimeZone];
//设置重复间隔,年,月,日,星期,都可以
noti.repeatInterval =kCFCalendarUnitDay;
//推送声音
noti.soundName =UILocalNotificationDefaultSoundName;
//内容
NSDictionary *infoDic =nil;
noti.alertBody =@"揽收报表更新了,快去看看吧!";
NSString *key = [NSStringstringWithFormat:@"lanshou_%@",time];
infoDic = @{key:@"揽收报表更新提醒"};
//显示在icon上的红色圈中的数子
noti.applicationIconBadgeNumber = 1;
//[UIApplication sharedApplication].applicationIconBadgeNumber += 1;//这是控制系统图标的数字,设置后就会显示的
//设置userinfo方便在之后需要撤销的时候使用
noti.userInfo = infoDic;
//添加推送到uiapplication
//iOS8之后需要添加这个才能授权注册
UIUserNotificationType type =UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
UIUserNotificationSettings* settings = [UIUserNotificationSettingssettingsForTypes:typecategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:settings];
UIApplication *app = [UIApplicationsharedApplication];
[app scheduleLocalNotification:noti];
}
}
//取消本地通知
-(void)cancelLocalNotificationWithKey:(NSString *)key {
// 获取所有本地通知数组
NSArray *localNotifications = [UIApplicationsharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notificationin localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
// 如果找到需要取消的通知,则取消
if (info !=nil) {
NSLog(@"取消了定时器%@",key);
[[UIApplicationsharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}
//取消所有通知的
[[UIApplicationsharedApplication]cancelAllLocalNotifications];
//在app delegate中写假如推送的时候,程序正在运行就会走这个里面
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"请在1小时内揽收订单"
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"好的"
otherButtonTitles:nil];
[alert show];
//这里,你就可以通过notification的useinfo,干一些你想做的事情了
application.applicationIconBadgeNumber -=1;
//不需要的时候可以直接取消这个推送
[[UIApplicationsharedApplication] cancelLocalNotification:notification];
}