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

时间:2022-08-05 01:06:29

So I'm basically trying to set an app that gives local notifications constantly.

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

So far I have:

到目前为止我有:

- (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];
    }
}

I'm trying to make it so I can get a notification every 2 minutes (when I set case 2) and every 1 minute (when I set case 1) to be notified. The only problem is... The *2 isn't working to make it get notified every 2 minutes. How would I go about making it so that it notifies every 2 minutes?

我试图这样做,所以我可以每隔2分钟(当我设置案例2时)和每1分钟(当我设置案例1时)收到通知。唯一的问题是...... * 2无法使其每2分钟得到通知。如何制作它以便每2分钟通知一次?

1 个解决方案

#1


2  

You can only use the defined calendar units in NSCalendarUnit when you are setting the repeatInterval property of a UILocalNotification. You can't use custom units or manipulate the units, so you won't be able to do what you want using the notification's repeat interval property.

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

To schedule notifications every 2 minutes, you will most likely want to schedule multiple notifications at different times (2 minutes apart). You can create a UILocalNotification, and then schedule it with:

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

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];

then modify the fireDate property (by adding your repeat interval), and then schedule it again with the same code. You can repeat this in a loop however many times you need to repeat the notification.

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

#1


2  

You can only use the defined calendar units in NSCalendarUnit when you are setting the repeatInterval property of a UILocalNotification. You can't use custom units or manipulate the units, so you won't be able to do what you want using the notification's repeat interval property.

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

To schedule notifications every 2 minutes, you will most likely want to schedule multiple notifications at different times (2 minutes apart). You can create a UILocalNotification, and then schedule it with:

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

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];

then modify the fireDate property (by adding your repeat interval), and then schedule it again with the same code. You can repeat this in a loop however many times you need to repeat the notification.

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