如何在某些日子重复UILocalNotification

时间:2021-05-15 21:44:51

I am making a alarm clock app that fires a local notification on a certain user selected day i.e. if user selected M, F, Sat then it should fire an alarm every M/F/Sat only. The problem that I have is that my bloody alarm is firing every single day at that time. How can I restrict it to user selected days? Here is my code

我正在制作一个闹钟应用程序,用于在特定用户选择的日期触发本地通知,即如果用户选择了M,F,Sat,那么它应该仅在每个M / F / Sat发出警报。我遇到的问题是那时我的血腥警报每天都在开火。如何将其限制为用户选择的日期?这是我的代码

//For testing purposes I want the alarm to fire only on Monday at 7:00am.
//This fires every day at 7:00am
[[UIApplication sharedApplication] cancelAllLocalNotifications];

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *now = [NSDate date];    

    // set components for time 7:00 a.m. Monday
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit |   NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];

    [componentsForFireDate setWeekday: 2] ;
    [componentsForFireDate setHour: 7] ;
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

    // Create the notification
    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;

    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Wake up!"] ;
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Waking Up time"] forKey:@"wakeUp"];

//Problem is here NSDayCalendarUnit makes my alarm fire every day
    notification.repeatInterval= NSDayCalendarUnit ;

    //notification.soundName=UILocalNotificationDefaultSoundName;
    notification.soundName=@"alarm-clock-ringing-01.wav";

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

2 个解决方案

#1


1  

Would it work if you actually make the local notification fire only once but when your device receives the notification, make it post the same notification again. Your app does not need to be running for this to work.

如果您实际上只发出一次本地通知但是当您的设备收到通知时,它会再次发布相同的通知吗?您的应用无需运行即可运行。

You handle the local notification when your application is not running, in the applicationDidFinishLaunching of your app delegate:

您的应用程序未运行时,您可以在应用程序委托的applicationDidFinishLaunching中处理本地通知:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

// handles notification when application is relaunched after being terminated
// not when app is already in the foreground running.
if(localNotif)
{   
    // repeat the same local notification again
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Note: repeatInterval will perform the same action by the unit you specified (in this case "day", will repeat every day regardless of what you specified as the date of the notification)

注意:repeatInterval将按您指定的单位执行相同的操作(在本例中为“day”,无论您指定的通知日期如何,都会每天重复)

#2


0  

You could make three alarms: one for the closest Monday, closest Wednesday and closest Friday and set notification.repeatInterval = NSWeekCalendarUnit on each of them.

您可以制作三个警报:一个用于最近的星期一,最近的星期三和最近的星期五,并在每个警报上设置notification.repeatInterval = NSWeekCalendarUnit。

#1


1  

Would it work if you actually make the local notification fire only once but when your device receives the notification, make it post the same notification again. Your app does not need to be running for this to work.

如果您实际上只发出一次本地通知但是当您的设备收到通知时,它会再次发布相同的通知吗?您的应用无需运行即可运行。

You handle the local notification when your application is not running, in the applicationDidFinishLaunching of your app delegate:

您的应用程序未运行时,您可以在应用程序委托的applicationDidFinishLaunching中处理本地通知:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

// handles notification when application is relaunched after being terminated
// not when app is already in the foreground running.
if(localNotif)
{   
    // repeat the same local notification again
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Note: repeatInterval will perform the same action by the unit you specified (in this case "day", will repeat every day regardless of what you specified as the date of the notification)

注意:repeatInterval将按您指定的单位执行相同的操作(在本例中为“day”,无论您指定的通知日期如何,都会每天重复)

#2


0  

You could make three alarms: one for the closest Monday, closest Wednesday and closest Friday and set notification.repeatInterval = NSWeekCalendarUnit on each of them.

您可以制作三个警报:一个用于最近的星期一,最近的星期三和最近的星期五,并在每个警报上设置notification.repeatInterval = NSWeekCalendarUnit。