gpt4 book ai didi

iphone - 如何创建每两分钟通知一次的 UILocalNotification

转载 作者:行者123 更新时间:2023-12-03 18:46:53 28 4
gpt4 key购买 nike

所以我基本上是在尝试设置一个不断提供本地通知的应用程序。

到目前为止我已经:

- (void)scheduleNotification {

[reminderText resignFirstResponder];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {

UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];

notif.alertBody = @"Your building is ready!";
notif.alertAction = @"View";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;

NSInteger index = [scheduleControl selectedSegmentIndex];
switch (index) {
case 1:
notif.repeatInterval = NSMinuteCalendarUnit;
break;
case 2:
notif.repeatInterval = NSMinuteCalendarUnit*2;
break;
default:
notif.repeatInterval = 0;
break;
}

NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}

我正在尝试做到这一点,以便我可以每 2 分钟(当我设置情况 2 时)和每 1 分钟(当我设置情况 1 时)收到通知。唯一的问题是... *2 无法使其每 2 分钟收到一次通知。我该如何使其每 2 分钟通知一次?

最佳答案

当您设置 UILocalNotification 的重复间隔属性时,只能使用 NSCalendarUnit 中定义的日历单位。您无法使用自定义单位或操纵单位,因此您将无法使用通知的重复间隔属性执行您想要的操作。

要每 2 分钟安排一次通知,您很可能希望在不同时间(间隔 2 分钟)安排多个通知。您可以创建一个 UILocalNotification,然后使用以下命令安排它:

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];

然后修改 fireDate 属性(通过添加重复间隔),然后使用相同的代码再次安排它。您可以循环重复此操作,无论您需要重复通知多少次。

关于iphone - 如何创建每两分钟通知一次的 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6895101/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com