如何创建每两分钟通知一次的UILocalNotification [英] How do I create a UILocalNotification that notifies every two minutes

查看:116
本文介绍了如何创建每两分钟通知一次的UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

到目前为止,我有:

- (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分钟通知一次?

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?

推荐答案

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

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.

每个计划通知2分钟,您很可能希望在不同时间(相隔2分钟)安排多个通知。您可以创建UILocalNotification,然后使用以下内容进行计划:

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

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

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.

这篇关于如何创建每两分钟通知一次的UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆