- (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 AM,2PM然后8PM
如果频率= 4 ..,则通知应触发8 AM,12PM,4PM,然后是8PM。

有办法吗?如果有人可以帮助我,那将很棒

最佳答案

不幸的是,您只能为NSCalendarUnit类型(天,周,月)的repeatInterval指定值。因此,我认为,您需要使用不同的fireDate创建多个Notifications,并为它们指定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/

10-09 20:48