// 定时推送的方法
- (void)addLocalNotification
{
// 创建一个本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
if ([[dict objectForKey:@"ID"] isEqualToString:self.home.ID]) {
[app cancelLocalNotification:noti];
break;
}
}
}
}
NSLog(@"%@", localArray);
if (notification != nil) {
// 设置推送时间
notification.fireDate = _datePicker.date;
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = 0;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
NSString *bodyStr = [NSString stringWithFormat:@"快来和我一起做%@吧😄", self.home.title];
notification.alertBody = bodyStr;
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:self.home.ID,@"ID", self.home.title, @"title", nil];
notification.userInfo = info;
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
}
网友评论