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

6 iphone ipad

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

到目前为止,我有:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Kei*_*ith 2

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

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

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];
Run Code Online (Sandbox Code Playgroud)

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