如何在iOS 8中14天后重复本地通知&;9
注册通知的代码为:如何在iOS 8中14天后重复本地通知&;9,ios,objective-c,Ios,Objective C,注册通知的代码为: UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
以及安排通知的代码:
UILocalNotification *notification = [[UILocalNotification alloc] init];
[self setNotificationTypesAllowed];
if (notification)
{
if (allowNotif)
{
notification.timeZone = [NSTimeZone defaultTimeZone];
if ( [statusString isEqualToString:@"daily"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitDay;
}else if ( [statusString isEqualToString:@"weekly"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitWeekOfYear;
}else if ( [statusString isEqualToString:@"fortnightly"]) {
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*14];;
notification.repeatInterval = NSCalendarUnitMinute;
//notification.repeatInterval = NSCalendarUnitYear;
}else{
notification.repeatInterval = 0;
}
}
if (allowsAlert)
{
notification.alertBody = [NSString stringWithFormat:@"Do you really want to send message to %@",name];
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.alertAction = @"Yes";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSMS) name:@"showSMS" object:nil];
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
我可以每天和每周重复本地通知,但不是每两周重复一次,请帮助将通知设置为每14天触发一次。您必须创建26个单独的UILocalNotifications,并将
重复间隔
设置为NSYearCalendarUnit
(重复间隔必须设置为日历单位,并且iOS中没有14天日历单位)
另一种处理方法是取消
didReceiveLocalNotification
方法中的UILocalNotification
,并在14天后安排一个新的通知。不过,此方法假定用户与通知交互,否则下一个通知将永远无法安排。将通知设置为每14天触发一次天数您必须创建26个单独的UILocalNotifications,并将repeatInterval
设置为NSYearCalendarUnit
(重复间隔必须设置为日历单位,并且iOS中没有14天日历单位)
另一种处理方法是取消
didReceiveLocalNotification
方法中的UILocalNotification
,并在14天后安排一个新的通知。不过,此方法假设用户与通知交互,否则下一个通知将永远无法安排。是@Nikos建议的,请尝试下面的方法e:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.userInfo = @{@"notification_identifier":@"After14Days"};
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
notification.alertBody = @"Text to display";
notification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
如果您有任何疑问,请告诉我。是@Nikos建议的,请尝试以下代码:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.userInfo = @{@"notification_identifier":@"After14Days"};
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
notification.alertBody = @"Text to display";
notification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
如果您有任何疑问,请告诉我。如果用户在30天内未打开应用程序,则上述代码不会在每14天后重复通知。但问题要求在每14天后重复通知,而不是一次。@RamaniAshish是的,您是对的,我忘了添加一行重复通知。我编辑了我的答案,您可以请立即查看。如果用户在30天内未打开应用程序,则上述代码不会在每14天后重复通知。但问题要求在每14天后重复通知,而不是一次。@RamaniAshish是的,您是对的,我忘记添加一行重复通知。我编辑了我的答案,您现在可以看到。