我想在 iOS 中的特定时间每天发送 2 个本地通知,
但是通知在错误的时间发送,而且一天发送多次而不是一次,
这是我的代码片段,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// are you running on iOS8?
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
application.applicationIconBadgeNumber = 0;
//This if will be called only once....
if ([prefs stringForKey:@"Notification"] == nil)
{
//... 1st notification ...
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:11];
[components setMinute:24];
// Gives us today's date but at 11am
NSDate *next11am = [calendar dateFromComponents:components];
if ([next11am timeIntervalSinceNow] < 0) {
// If today's 9am already occurred, add 24hours to get to tomorrow's
next11am = [next11am dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = next11am;
notification.alertBody = @"Notification 1.";
// Set a repeat interval to daily
notification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//... 2nd Notification ...
NSDate *now1 = [NSDate date];
NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [calendar1 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now1];
[components1 setHour:18];
[components1 setMinute:30];
// Gives us today's date but at 9am
NSDate *next6pm = [calendar dateFromComponents:components];
if ([next6pm timeIntervalSinceNow] < 0) {
// If today's 6pm already occurred, add 24hours to get to tomorrow's
next6pm = [next6pm dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *notification1 = [[UILocalNotification alloc] init];
notification1.fireDate = next6pm;
notification1.alertBody = @"Notification 2.";
// Set a repeat interval to daily
notification1.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification1];
}
}
我哪里做错了?请帮忙
提前致谢!!
最佳答案
你检查了[prefs stringForKey:@"Notification"]
,但是你还没有定义prefs
所以它可能是nil,而且你从来没有设置值对于 Notification
这样也可能会丢失。任何这些都意味着您在不期望的时候添加。
您正在调用 scheduleLocalNotification:
但您没有在任何地方调用 cancelLocalNotification:
或 cancelAllLocalNotifications
因此,根据用户使用应用程序的方式,或者您如何进行测试,您可以轻松地同时添加多个通知。使用 scheduledLocalNotifications
检查当前安排的内容。
您可能还应该使用 currentCalendar
作为日历,因为您需要尊重用户设置。
关于ios - 本地通知在错误的时间发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30185487/