如何在 objc 中以重复间隔每 15 天触发一次 UILocalNotification
How can I fire a UILocalNotification for every 15 days with repeat interval in objc
我正在尝试在早上 6 点每隔 15 次重复触发一次 uilocalnotification。
我试过示例代码,但不确定如何在模拟器中测试它。
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[components setHour:6];
[components setMinute:0];
[components setSecond:0];
// Gives us today's date but at 6am
NSDate *next6am = [calendar dateFromComponents:components];
NSLog(@"next9am BEFORE :%@",next6am);
if ([next6am timeIntervalSinceNow] < 0) {
// If today's 6am already occurred, add 24hours to get to tomorrow's
next6am = [next6am dateByAddingTimeInterval:60*60*24*15];
}
UILocalNotification* Localnotification = [[UILocalNotification alloc]init];
Localnotification.fireDate = next6am;
Localnotification.alertBody = @“it is been 15 days“;
Localnotification.repeatInterval = NSCalendarUnitWeekOfYear*2+NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:Localnotification];
但不确定如何在模拟器中测试它,也不确定它是否有效
帮助将不胜感激。
谢谢
本地通知适用于模拟器。但是,如果您想在应用程序处于前台时看到通知,请确保您在应用程序委托中实现了 application:didreceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyAlertView" message:notification.alertBody delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
在互联网上进行大量搜索并尝试变通后,我了解到 iOS 重复间隔如下,
NSCalendarUnitYear // for every year
NSCalendarUnitMonth // for every month
NSCalendarUnitDay // for every day
NSCalendarUnitHour // for every hour
NSCalendarUnitMinute // for every minute
and many more
我们只能按照上面的方式设置重复间隔。
但是如果我们想每 15 天重复一次间隔,我们需要为 UILocalNotification 编写一个自定义重复间隔。
所以每年有 12 个月,要安排的活动数量是 24,所以我循环了 2 年(730 天)并为接下来的 2 年安排了 48 场活动
下面是实现结果的代码
UILocalNotification* Localnotification = [[UILocalNotification alloc]init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *components =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
[components setHour:6];
[components setMinute:0];
[components setSecond:0];
// Gives us today's date
NSDate *next6am = [calendar dateFromComponents:components];
/*
* Since every year has 365 days and
*/
for (int i = 0; i<48; i++)//
{
next6am = [next6am dateByAddingTimeInterval:60*60*24*15];
Localnotification.fireDate = next6am;
//////add the userinfo for uilocal notification only after fire date. if called before the user info will not be updated.
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"15DaysAlerts"] forKey:@"notify"];
Localnotification.userInfo = infoDict;
if (CHECK_IOS_VERSION>8.2)
{
Localnotification.alertTitle = @"eKinCare";
}
Localnotification.alertBody = @"Any new medical reports to save?Touch to securely save & access anywhere, anytime.";
// Set a repeat interval to monthly
Localnotification.repeatInterval = NSCalendarUnitYear;
[[UIApplication sharedApplication] scheduleLocalNotification: Localnotification];
NSLog(@"next9am:%@",next6am);
}
您可以使用以下方法查看所有已安排的活动
-(void)showScheduledNotifications
{
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
NSLog(@"Events array : %@",eventArray);
int scheduled_15daysAlerts = 0;
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"notify"]];
if ([uid isEqualToString:@"15DaysAlerts"])
{
scheduled_15daysAlerts++;
NSLog(@"yes found");
//Cancelling local notification
}
}
NSLog(@"Scheduled 15 days alert = [%d]",scheduled_15daysAlerts);
}
希望这对正在寻找解决方案的人有所帮助:)
在iOS 10中,您可以使用UNTimeIntervalNotificationTrigger
安排每15天通知一次并重复(设置repeats: YES
)。
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(15*24*3600) repeats: YES];
NSLog(@"td %@", trigger.nextTriggerDate);
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
} else {
NSLog(@"Created! --> %@",request);
}
}];
希望这对寻找此主题的任何人有所帮助。
我正在尝试在早上 6 点每隔 15 次重复触发一次 uilocalnotification。
我试过示例代码,但不确定如何在模拟器中测试它。
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[components setHour:6];
[components setMinute:0];
[components setSecond:0];
// Gives us today's date but at 6am
NSDate *next6am = [calendar dateFromComponents:components];
NSLog(@"next9am BEFORE :%@",next6am);
if ([next6am timeIntervalSinceNow] < 0) {
// If today's 6am already occurred, add 24hours to get to tomorrow's
next6am = [next6am dateByAddingTimeInterval:60*60*24*15];
}
UILocalNotification* Localnotification = [[UILocalNotification alloc]init];
Localnotification.fireDate = next6am;
Localnotification.alertBody = @“it is been 15 days“;
Localnotification.repeatInterval = NSCalendarUnitWeekOfYear*2+NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:Localnotification];
但不确定如何在模拟器中测试它,也不确定它是否有效 帮助将不胜感激。
谢谢
本地通知适用于模拟器。但是,如果您想在应用程序处于前台时看到通知,请确保您在应用程序委托中实现了 application:didreceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyAlertView" message:notification.alertBody delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
在互联网上进行大量搜索并尝试变通后,我了解到 iOS 重复间隔如下,
NSCalendarUnitYear // for every year
NSCalendarUnitMonth // for every month
NSCalendarUnitDay // for every day
NSCalendarUnitHour // for every hour
NSCalendarUnitMinute // for every minute
and many more
我们只能按照上面的方式设置重复间隔。
但是如果我们想每 15 天重复一次间隔,我们需要为 UILocalNotification 编写一个自定义重复间隔。
所以每年有 12 个月,要安排的活动数量是 24,所以我循环了 2 年(730 天)并为接下来的 2 年安排了 48 场活动
下面是实现结果的代码
UILocalNotification* Localnotification = [[UILocalNotification alloc]init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *components =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
[components setHour:6];
[components setMinute:0];
[components setSecond:0];
// Gives us today's date
NSDate *next6am = [calendar dateFromComponents:components];
/*
* Since every year has 365 days and
*/
for (int i = 0; i<48; i++)//
{
next6am = [next6am dateByAddingTimeInterval:60*60*24*15];
Localnotification.fireDate = next6am;
//////add the userinfo for uilocal notification only after fire date. if called before the user info will not be updated.
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"15DaysAlerts"] forKey:@"notify"];
Localnotification.userInfo = infoDict;
if (CHECK_IOS_VERSION>8.2)
{
Localnotification.alertTitle = @"eKinCare";
}
Localnotification.alertBody = @"Any new medical reports to save?Touch to securely save & access anywhere, anytime.";
// Set a repeat interval to monthly
Localnotification.repeatInterval = NSCalendarUnitYear;
[[UIApplication sharedApplication] scheduleLocalNotification: Localnotification];
NSLog(@"next9am:%@",next6am);
}
您可以使用以下方法查看所有已安排的活动
-(void)showScheduledNotifications
{
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
NSLog(@"Events array : %@",eventArray);
int scheduled_15daysAlerts = 0;
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"notify"]];
if ([uid isEqualToString:@"15DaysAlerts"])
{
scheduled_15daysAlerts++;
NSLog(@"yes found");
//Cancelling local notification
}
}
NSLog(@"Scheduled 15 days alert = [%d]",scheduled_15daysAlerts);
}
希望这对正在寻找解决方案的人有所帮助:)
在iOS 10中,您可以使用UNTimeIntervalNotificationTrigger
安排每15天通知一次并重复(设置repeats: YES
)。
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(15*24*3600) repeats: YES];
NSLog(@"td %@", trigger.nextTriggerDate);
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
} else {
NSLog(@"Created! --> %@",request);
}
}];
希望这对寻找此主题的任何人有所帮助。