iOS在特定日期设置本地通知 [英] iOS set local notification on a specific day

查看:225
本文介绍了iOS在特定日期设置本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这个问题在某处重复,但我找不到解决方案。我正在创建一个应用程序,其中一个功能允许用户选择他们将收到本地通知的日期和时间。

I am sure this question is duplicated somewhere, but I can't find a solution. I am making an app in which one feature allows the user to select the days and times they will receive a local notification.

他们可以选择他们喜欢的一天中的任何时间,并可以切换一周的不同日子(周一,周二,婚礼等)。通知将每周发送一次。因此,我限制用户只创建3个通知 - 如果选择了所有7天,我会将 repeatInterval 设置为每日(一个通知)。如果为每3个通知选择了6天,那么我将需要每天的单独通知(总共3x6 = 18个通知)。很可能只会使用1个通知,所以这很好。

They can select any time of the day they like, and can toggle the different days of the week (mon, tues, weds etc). The notifications will be sent weekly. I therefore limit the user to creating just 3 notifications - if all 7 days are selected I will set the repeatInterval to daily (one notification). If 6 days are selected for each 3 notifications then I will need an individual notification for each day (totalling 3x6=18 notifications). In all likelihood, only 1 notification will be used so this is fine.

我知道如何在将来的某个时间设置通知,但我该如何设置星期一下午6点的通知?

I know how to set an notification for a certain time in the future, but how do I set a notification for say 6pm on a Monday?

以下是我用于测试的代码。它在未来设置了4秒的警报(我从 applicationDidEnterBackground 调用它)。

Below is my code which I have been using for testing. It sets an alert for 4 seconds in the future (I was calling it from applicationDidEnterBackground).

    NSDateComponents *changeComponent = [[NSDateComponents alloc] init];
    changeComponent.second = 4;

    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    NSDate *itemDate = [theCalendar dateByAddingComponents:changeComponent toDate:[NSDate date] options:0];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.repeatInterval = NSWeekdayCalendarUnit;


推荐答案

与你现在的做法完全一样,但是以不同方式创建日期:

Exactly the same way you are doing right now, but create a date differently:

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:2013];
[comps setHour:10];
[comps setMinute:10];
[comps setSecond:10];
localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:comps];

这篇关于iOS在特定日期设置本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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