ios - 如何在 UILocalnotification 中分配自定义时间?

标签 ios objective-c nstimer uilocalnotification

    - (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/

相关文章:

ios - 用户强行终止应用程序 : Registering this event

ios - 如何在 iOS 上运行 Tensorflow 对象检测

ios - 如何在Swift 5中验证用户的时间是正确的并且没有欺骗应用程序?

ios - 未使用本地化文件

ios - 在发件人中使用带有 UIButton 的标签属性并尝试转换 id -> UIButton

ios - 在 iOS 6 及更高版本中手动调用状态保存

iphone - 以编程方式枚举 UIViewController 的传出 Segue

c++ - Xcode C++ 和 Objective-C 重构

ios - NStimer 和 setNeedsDisplay

objective-c - 使用 NSTimer 传递原始参数的正确方法