我试图在每天早上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];
但不确定如何在模拟器中测试它并且不确定它是否会起作用 将不胜感激。
由于
答案 0 :(得分:1)
本地通知适用于模拟器。但是,如果您希望在应用程序位于前台时看到nofication,请确保在应用委托中实现应用程序: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];
}
}
答案 1 :(得分:1)
经过大量搜索互联网并尝试解决后,我才知道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);
}
希望这可以帮助正在寻找解决方案的人:)
答案 2 :(得分:0)
在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);
}
}];
希望这可以帮助任何寻找此话题的人。