- (void)scheduleNotification :(int) rowNo
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.timeZone = [NSTimeZone defaultTimeZone];
NSString *descriptionBody=[[remedyArray objectAtIndex:rowNo]objectForKey:@"RemedyTxtDic"];
NSLog(@"%@",descriptionBody);
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
forKey:@"kRemindMeNotificationDataKey"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
我有一个从 Sqldb 获取的列名频率,其中包含特定单元格应出现通知的次数。 如果频率 = 3 ..通知应该在上午 8 点、下午 2 点然后晚上 8 点触发 如果频率 = 4 ..通知应该在上午 8 点、中午 12 点、下午 4 点然后晚上 8 点触发。
有办法吗?如果有人能帮助我那就太好了
最佳答案
不幸的是,您只能为 NSCalendarUnit 类型(日、周、月)的重复间隔指定值。所以,我认为,您需要创建多个具有不同 fireDate 的通知,并为它们指定repeatInterval = NSDayCalendarUnit 例如,
NSDate *currentTime = [NSDate date];
notification1.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL sinceDate:currentTime];
notification1.repeatInterval = NSDayCalendarUnit;
notification2.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL * 2 sinceDate:currentTime];
notification2.repeatInterval = NSDayCalendarUnit;
用户查看一些通知后 - 您可以取消它们。
更新。
您还可以从不同的组件创建 NSDate,如下所示:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2013];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
您还可以设置时、分、秒、时区和其他参数。
关于ios - 如何在 UILocalnotification 中分配自定义时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14457858/