[英]local notification are sent at wrong time
I want to sent 2 local notifications daily at a particular time in iOS, 我想每天在特定时间在iOS中发送2条本地通知,
But notifications are sent at wrong time and also it sends multiple times in day and not once, 但是通知是在错误的时间发送的,而且一天中会发送多次,而不是一次,
Here is my code snippet, 这是我的代码段,
- (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];
}
}
Where i am doing mistake ? 我在哪里做错了? please help
请帮忙
Thanks in advance!! 提前致谢!!
You check [prefs stringForKey:@"Notification"]
, but you haven't defined prefs
so it could be nil, and you never appear to set the value for Notification
so that could also be missing. 您检查
[prefs stringForKey:@"Notification"]
,但是尚未定义prefs
因此它可能为nil,而且您似乎从未设置Notification
的值,因此也可能会丢失。 Any of these things would mean you're adding when you don't expect to. 这些事情中的任何一个都意味着您在意料之外的时候添加。
You're calling scheduleLocalNotification:
but nowhere are you calling cancelLocalNotification:
or cancelAllLocalNotifications
so, depending on how the user uses the app, or how you're doing your testing, you can easily be adding multiple notifications at the same time. 您正在调用
scheduleLocalNotification:
但您在哪里都没有调用cancelLocalNotification:
或cancelAllLocalNotifications
因此,根据用户使用应用程序的方式或测试方式的不同,您可以轻松地同时添加多个通知。 Use scheduledLocalNotifications
to check what is currently scheduled. 使用
scheduledLocalNotifications
检查当前计划的内容。
You should probably also be using currentCalendar
for the calendar as you need to respect the users settings. 您可能还应该为日历使用
currentCalendar
,因为您需要尊重用户设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.