两个本地通知错误

Posted

技术标签:

【中文标题】两个本地通知错误【英文标题】:Two Local Notifications Error 【发布时间】:2013-06-18 11:34:14 【问题描述】:

我正在制作一个使用本地通知的应用程序。我正在尝试并使用两个实例。当我运行杀死应用程序时,只显示一个通知。这是应用程序delegate.m中的代码

- (void)applicationDidEnterBackground:(UIApplication *)application



//1
NSCalendar *gregcalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *datecomponent2 = [gregcalendar2 components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

[datecomponent2 setYear:2013];
[datecomponent2 setMonth:6];
[datecomponent2 setDay:18];
[datecomponent2 setHour:16];
[datecomponent2 setMinute:58];

UIDatePicker *dd2 = [[UIDatePicker alloc]init];
[dd2 setDate:[gregcalendar2 dateFromComponents:datecomponent2]];


UILocalNotification *notification2 = [[UILocalNotification alloc]init];
[notification2 setAlertBody:@"two"];
[notification2 setFireDate:dd2.date];
[notification2 setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification2]];


//2

NSCalendar *gregcalendar1 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *datecomponent1 = [gregcalendar1 components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

[datecomponent1 setYear:2013];
[datecomponent1 setMonth:6];
[datecomponent1 setDay:18];
[datecomponent1 setHour:16];
[datecomponent1 setMinute:59];

UIDatePicker *dd1 = [[UIDatePicker alloc]init];
[dd1 setDate:[gregcalendar1 dateFromComponents:datecomponent1]];


UILocalNotification *notification1 = [[UILocalNotification alloc]init];
[notification1 setAlertBody:@"one"];
[notification1 setFireDate:dd1.date];
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification1]];

【问题讨论】:

【参考方案1】:

首先,你这样做:

[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification2]];

然后

[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification1]];

这意味着:用包含notification1的数组替换所有之前安排的本地通知(在本例中为notification2)。

你可以使用:

[application scheduleLocalNotification:notification1];
[application scheduleLocalNotification:notification2];

或:

[application setScheduledLocalNotifications:@[notification1, notification2]];

【讨论】:

以上是关于两个本地通知错误的主要内容,如果未能解决你的问题,请参考以下文章

ios重复本地通知

无法在点击事件时设置离子本地通知(奇怪的语法错误)

iOS 添加本地通知时 App 发生崩溃提示 4097 UserNotificationService 错误的解决

iOS 添加本地通知时 App 发生崩溃提示 4097 UserNotificationService 错误的解决

date.getTime 不是函数离子本地通知错误

本地通知问题?