Ios 在不同日期触发多个UILocalNotification
嗨,在我的应用程序中,我想为多个事件显示Ios 在不同日期触发多个UILocalNotification,ios,oop,uilocalnotification,Ios,Oop,Uilocalnotification,嗨,在我的应用程序中,我想为多个事件显示UILocalNotification。比如母亲节,新年等等。所以我已经有了所有事件的日期,我不想从用户那里得到任何事件日期。我需要的是在不同月份的特定日期显示UILocalNotification,请告诉我可以这样做,以及如何做 我已经厌倦了这种单一的UILocaNotification - (void)applicationDidEnterBackground:(UIApplication *)application { NSCal
UILocalNotification
。比如母亲节,新年等等。所以我已经有了所有事件的日期,我不想从用户那里得到任何事件日期。我需要的是在不同月份的特定日期显示UILocalNotification
,请告诉我可以这样做,以及如何做
我已经厌倦了这种单一的UILocaNotification
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSCalendar *regCalender =[NSCalendar currentCalendar];
NSDateComponents *dateComponent = [regCalender components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent setYear:2014];
[dateComponent setMonth:7];
[dateComponent setDay:9];
[dateComponent setHour:16];
[dateComponent setMinute:0];
UIDatePicker *dd = [[UIDatePicker alloc]init];
[dd setDate:[regCalender dateFromComponents:dateComponent]];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"Welcome"];
[notification setFireDate:dd.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
notification.soundName=@"double_tone.mp3";
NSCalendar *regCalender1 =[NSCalendar currentCalendar];
NSDateComponents *dateComponent1 = [regCalender components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent1 setYear:2014];
[dateComponent1 setMonth:7];
[dateComponent1 setDay:10];
[dateComponent1 setHour:16];
[dateComponent1 setMinute:0];
UIDatePicker *dd1 = [[UIDatePicker alloc]init];
[dd setDate:[regCalender1 dateFromComponents:dateComponent]];
UILocalNotification *notification1 = [[UILocalNotification alloc]init];
[notification1 setAlertBody:@"home"];
[notification1 setFireDate:dd1.date];
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]];
notification1.soundName=@"double_tone.mp3";
[application setScheduledLocalNotifications:@[ notification, notification1 ]];
}
我已经在我的Appdelegate
中为单个UILocalNotification
使用了此代码。请告诉我如何处理多个。我试过类似的方法,但不起作用
[application setScheduledLocalNotifications:@[ notification, notification1 ]];
执行此操作时:
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification1]];
您每次都在替换所有通知
相反,请尝试:
[application setScheduledLocalNotifications:@[ notification, notification1 ]];
您可以创建一个包含所有通知的阵列,并同时安排它们
另一种方法是多次调用scheduleLocalNotification:
,但这效率较低。在执行SetScheduledLocalNotification时,
数组参数需要包含多个通知项。@Petesh我是ios开发的新手,请告诉我如何将数组通知传递给that@Petesh你能给我一些样品吗?这不是iOS特有的问题。您需要创建多个本地通知实例,将它们全部添加到一个数组中(而不仅仅是当前创建的一个),然后传递要调度的数组。你明白“实例”是什么吗?@Wain我试过类似的方法,但它不起作用。[应用程序集ScheduledLocalNotification:[NSArray arrayWithObject:notification];[应用程序集ScheduledLocalNotification:[NSArray arrayWithObject:notification1]];请检查我编辑的问题,下一秒我缩小应用程序时,它仍然不工作。它显示通知。它不工作。请解释您是如何测试的。通知设置为什么日期以及您测试的日期(具体时间很重要)我正在尝试将时间视为常见时间,但日期和月份不同,例如母亲节愿望妇女节和父亲节,我需要您指示您创建一些通知,安排它们,然后关闭应用程序进行测试。对的如果是这样的话,您如何等待“几个月”才能在指定日期看到通知?否,我将在系统中更改日期,并在通知生效意味着易于测试时对其进行测试