Par*_*att 24 iphone cocoa-touch ios4 localnotification uilocalnotification
我正在制作一个iPhone应用程序,它需要本地通知.
在本地通知中有repeatInterval属性,我们可以将单位重复间隔设置为分钟,小时,日,周,年等.
我希望重复间隔应该是4个小时.
所以每4小时就有一次本地通知.
我不希望用户为每个设置单独的通知.
我希望用户能够将repeatInterval设置为4小时.
我怎么做?
Par*_*att 43
得到了答案
它就像它一样直.
您无法创建自定义重复间隔.
您必须使用NSCalendarUnit的内置单位时间间隔.
我尝试了所有上述解决方案,甚至尝试了其他的东西,但他们都没有工作.
我已经投入了大量时间来发现我们无法让它在自定义时间间隔内工作.
希望这有助于所有寻求解决方案的人.
感谢所有试图回答我问题的人.多谢你们!!
Lav*_*der 18
重复间隔只是一个具有位图常量的枚举,因此无法自定义repeatIntervals
,只需每分钟,每秒,每周等.
话虽这么说,你的用户没有理由设置每一个.如果您让他们在您的用户界面中设置两个值,例如" Frequency unit: Yearly/Monthly/Weekly/Hourly
"和" Every ____ years/months/weeks/hours
",则可以通过设置适当的开火日期而不重复来自动生成相应的通知.
UILocalNotification *locNot = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
NSInterval interval;
switch( freqFlag ) { // Where freqFlag is NSHourCalendarUnit for example
case NSHourCalendarUnit:
interval = 60 * 60; // One hour in seconds
break;
case NSDayCalendarUnit:
interval = 24 * 60 * 60; // One day in seconds
break;
}
if( every == 1 ) {
locNot.fireDate = [NSDate dateWithTimeInterval: interval fromDate: now];
locNot.repeatInterval = freqFlag;
[[UIApplication sharedApplication] scheduleLocalNotification: locNot];
} else {
for( int i = 1; i <= repeatCountDesired; ++i ) {
locNot.fireDate = [NSDate dateWithTimeInterval: interval*i fromDate: now];
[[UIApplication sharedApplication] scheduleLocalNotification: locNot];
}
}
[locNot release];
Run Code Online (Sandbox Code Playgroud)
我有一个想法如何做到这一点,我已经在我的项目中实现了这一点
首先,使用开火日期(例如,每分钟)创建本地通知.下一步 - 使用此通知的唯一ID填写用户信息(如果您希望将来删除它)以及您的自定义期间,如下所示:
-(void) createLocalRepeatedNotificationWithId: (NSString*) Id
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSTimeInterval your_custom_fire_interval = 60; // interval in seconds
NSDate *remindDate = [[NSDate date] dateByAddingTimeInterval:your_custom_fire_interval];
localNotification.fireDate = remindDate;
localNotification.userInfo = @{@"uid":Id, @"period": [NSNumber numberWithInteger:your_custom_fire_interval]};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Run Code Online (Sandbox Code Playgroud)
之后,-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
在AppDelegate中实现:
只需将它添加到隐藏的通知中即可!
NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1
NSTimeInterval t= 10 * period;
notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3
Run Code Online (Sandbox Code Playgroud)如果要删除此通知,请执行此操作
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"id"]];
if ([uid isEqualToString:notification_id_to_remove])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
很重要!
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
当你在后台时不要打电话.因此,您必须设置长时间运行的后台任务,您将在其中再次创建通知.
我已经使用了这段代码,它可以正常重复LocalNotification我已经使用LavaSlider代码进行此代码实现
UILocalNotification * localNotifEndCycle = [[UILocalNotification alloc] init];
localNotifEndCycle.alertBody = @"Your Expected Date ";
NSDate *now = [NSDate date];
for( int i = 1; i <= 10;i++)
{
localNotifEndCycle.alertBody = @"Your Expected Date ";
localNotifEndCycle.soundName=@"best_guitar_tone.mp3";
localNotifEndCycle.fireDate = [NSDate dateWithTimeInterval:180*i sinceDate:now];
[[UIApplication sharedApplication] scheduleLocalNotification: localNotifEndCycle];
}
}
Run Code Online (Sandbox Code Playgroud)