如何将本地通知重复间隔设置为自定义时间间隔?
Posted
技术标签:
【中文标题】如何将本地通知重复间隔设置为自定义时间间隔?【英文标题】:How to set Local Notification repeat interval to custom time interval? 【发布时间】:2010-12-06 06:30:22 【问题描述】:我正在制作一个需要本地通知的 iPhone 应用。
在本地通知中有 repeatInterval 属性,我们可以将单位重复间隔设置为分钟、小时、天、周、年等。
我希望重复间隔应该是 4 小时。
所以每 4 小时本地通知来一次。
我不希望用户为每个设置单独的通知。
我希望用户能够将 repeatInterval 设置为 4 小时。
我该怎么做?
【问题讨论】:
【参考方案1】:得到了答案,它是直截了当的。
您无法创建自定义重复间隔。
您必须使用 NSCalendarUnit 的内置单位时间间隔。
我尝试了上述所有解决方案,甚至尝试了其他方法,但都没有奏效。
我已经投入了大量时间来发现我们无法让它在自定义时间间隔内工作。
【讨论】:
但是,请参阅answer。如果您可以接受 64 条通知限制,它提供了一种实现此目的的方法。 您可以尝试设置每天的重复间隔,然后删除所需天数的通知。 我不满意这个答案,因为当应用程序处于后台并且 VOIP 呼叫到来时,whatsapp 每 3-4 秒显示一次通知【参考方案2】:重复间隔只是一个具有位图常量的枚举,因此无法自定义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];
【讨论】:
我们可以以任何方式使用 repeatInterval 3、5、7 天的间隔吗? Apples 提醒应用程序如何安排特定星期或每 x 天的重复间隔等。我怀疑他们是在安排每一个,还是?他们的应用也会用完 64 个分配限制【参考方案3】:我有一个想法如何做到这一点,我已经在我的项目中实现了这一点
首先,创建带有触发日期的本地通知(例如,每分钟)。 下一步 - 使用此通知的唯一 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];
之后,在你的 AppDelegate 中实现-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
:
-
从用户信息中获取您的自定义期间。
更改下一个周期的触发日期
只需再次将其添加到隐藏的通知中!
NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1
NSTimeInterval t= 10 * period;
notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3
如果您想删除此通知,请执行
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;
非常重要!
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
在后台时不要调用。因此,您必须设置长时间运行的后台任务,您将在其中再次创建通知。
【讨论】:
:: 非常好...你能帮我看看当我的应用程序处于后台或终止模式时调用哪个方法吗?因为我想在基于“uid”的通知横幅上显示动态文本。尽快重播我 @BandishDave, - (void)applicationWillResignActive:(UIApplication *)application - 调用此方法是为了让您的应用知道它即将从活动状态转移到非活动状态。这可能发生在某些类型的临时中断中。另外,请参阅 - (void)applicationWillTerminate:(UIApplication *)application - 此方法让您的应用知道它即将被终止并完全从内存中清除。【参考方案4】:我已经使用了这个代码,它可以很好地重复 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];
【讨论】:
【参考方案5】:-(void)schedulenotificationfortimeinterval:(NSString *)id1
NSLog(@"selected value %d",selectedvalue);
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MMM dd,yyyy hh:mm a"];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
// localNotification2. fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:0];
if(selectedvalue==0)
NSLog(@"dk 0000");
localNotification2.fireDate = [formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] ;//now
localNotification2.applicationIconBadgeNumber = 1;
localNotification2.repeatInterval=NSDayCalendarUnit;
if(selectedvalue==1)
NSLog(@"dk 1111");
for( int u = 0; u <= 2 ;u++)
localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:43200.0*u] ;// 12 hr
localNotification2.repeatInterval=NSDayCalendarUnit;
localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]];
localNotification2.alertAction = @"Notification";
localNotification2.soundName=UILocalNotificationDefaultSoundName;
localNotification2.applicationIconBadgeNumber = 2;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
// localNotification2.repeatInterval=NSDayCalendarUnit;
if(selectedvalue==2)
NSLog(@"dk 22222");
for( int u = 0; u <= 3 ;u++)
localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:28800.0*u] ;//8 hr
localNotification2.repeatInterval=NSDayCalendarUnit;
localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]];
localNotification2.alertAction = @"Notification";
localNotification2.soundName=UILocalNotificationDefaultSoundName;
localNotification2.applicationIconBadgeNumber = 3;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
// localNotification2.repeatInterval=NSDayCalendarUnit;
if(selectedvalue==3)
NSLog(@"dk 3333");
for( int u = 0; u <= 4 ;u++)
localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:21600.0*u] ;//6 hr
localNotification2.repeatInterval=NSDayCalendarUnit;
localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]];
localNotification2.alertAction = @"Notification";
localNotification2.soundName=UILocalNotificationDefaultSoundName;
localNotification2.applicationIconBadgeNumber = 4;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
// localNotification2.repeatInterval=NSDayCalendarUnit;
// localNotification2.repeatInterval=NSDayCalendarUnit;
NSLog(@"date is %@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]);
localNotification2.timeZone = [NSTimeZone localTimeZone];
localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]];
localNotification2.alertAction = @"Notification";
localNotification2.soundName=UILocalNotificationDefaultSoundName;
//localNotification2.applicationIconBadgeNumber = 1;
// infoDict = [NSDictionary dictionaryWithObject:id1 forKey:@"did"];
// localNotification2.userInfo = infoDict;
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
// NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
// [[UIApplication sharedApplication] cancelAllLocalNotifications];//dk
【讨论】:
【参考方案6】:如果您为每次想要触发通知的时间设置单独的通知,您可以设置任何所需的时间间隔。然后您必须管理它们,如果您不是当前允许在后台运行的应用程序,则必须通过让用户运行您的应用程序来刷新它们。完成此操作后,我可以告诉您这是一个主要的 PITA。
【讨论】:
【参考方案7】://This is how I set local notification based on time interval repeatedly and executed method customized snooze and delete
let content = UNMutableNotificationContent()
content.title = "Time Based Local Notification"
content.subtitle = "Its is a demo"
content.sound = .default
content.categoryIdentifier = "UYLReminderCategory"
//repition of time base local notification in foreground, background, killed
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "ios Demo", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "UYLDeleteAction", title: "Delete", options: [.destructive])
let cat = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction,deleteAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([cat])
【讨论】:
你能添加描述吗?【参考方案8】:notif.repeatInterval = NSDayCalendarUnit;
【讨论】:
以上是关于如何将本地通知重复间隔设置为自定义时间间隔?的主要内容,如果未能解决你的问题,请参考以下文章