开发者

How do I create a UILocalNotification that notifies every two minutes

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 select开发者_开发问答edSegmentIndex];
        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?


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.

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:

[[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜