我想要有两个本地通知,两者都有不同的时间,让我说我的第一个通知会在1分钟后发出警报,第二个会在2分钟后发出警报。
我试过在appDelegate中创建两个,但只有第一个给我通知而不是第二个。
我怎样才能做到这一点?
答案 0 :(得分:2)
是可以在任何iOS应用程序中设置两个LocalNotification
请参阅以下方法,您可以通过该方法设置多个LocalNotifications
您只需要将所需参数传递给此方法。
- (void)setAlarmFor:(NSArray*)datesArray forTime:(NSString*)atTime notificationName:(NSString*)name
{
for(int dayIndex=0;dayIndex <[datesArray count];dayIndex++)
{
Class cls = NSClassFromString(@"UILocalNotification");//
if (cls != nil) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
NSString* dateStr=[datesArray objectAtIndex:dayIndex];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *tempDate = [dateFormatter dateFromString:dateStr];
NSString *tempString = [dateFormatter stringFromDate:tempDate];
tempString = [NSString stringWithFormat:@"%@ %@",tempString,atTime];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSDate *firetAtThisDate = [dateFormatter dateFromString:tempString];
UILocalNotification *localNotif = [[cls alloc] init];
localNotif.fireDate =firetAtThisDate;//here set the Date at which mnotification fire;
NSLog(@"Notification date is:%@",firetAtThisDate);
localNotif.alertBody =name;
localNotif.alertAction = @"Your'Alert message";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:tempString
forKey:tempString];//by using this we can further cancel the Notification
localNotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[dateFormatter release];
}
}
}
并在Appdelegate课程中准备动作你想要的通知消息
//This Below Line will goes to the Appdelegate DidFinishLaunching Method
Class cls = NSClassFromString(@"UILocalNotification");
if (cls)
{
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
//do what you want
}
}
application.applicationIconBadgeNumber = 0;
//End of Appdelegate DidFinishLaunching Method.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
//do what you want
}
答案 1 :(得分:0)
答案 2 :(得分:0)
当然,您在应用中使用了一个或多个本地通知。在您的项目中尝试此代码
-(void) setLocalNotification
{
NSTimeInterval todayTimeIntervel=[[NSDate date]timeIntervalSince1970];
NSTimeInterval nextOneMinTimeIntervel;
nextOneMinTimeIntervel = todayTimeIntervel + 60 ;
NSTimeInterval nexttwoMinTimeIntervel;
nexttwoMinTimeIntervel = todayTimeIntervel + 60*3;
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:nextOneMinTimeIntervel];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:nexttwoMinTimeIntervel];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSString *strDate1 = [dateFormat stringFromDate:date1];
NSString *strDate2 = [dateFormat stringFromDate:date2];
NSArray *arr = [NSArray arrayWithObjects:strDate1,strDate2, nil];
NSArray *titleArr = [NSArray arrayWithObjects:@"First LocalNotification",@"Second LocalNotification", nil];
for (int i =0; i < 2; i++)
{
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[arr objectAtIndex:i],@"dateStr",[titleArr objectAtIndex:i],@"title", nil];
[self scheduleLocalNotification:dic];
}
}
-(void) scheduleLocalNotification:(NSMutableDictionary*) dic
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSLog(@"%@", [dateFormat dateFromString:[dic objectForKey:@"dateStr"]]);
localNotif.fireDate = [dateFormat dateFromString:[dic objectForKey:@"dateStr"]];
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertAction = @"View";
localNotif.alertBody = [dic objectForKey:@"title"];
localNotif.userInfo = dic;
NSLog(@"value of infoDic %@",dic);
localNotif.repeatInterval = NSDayCalendarUnit;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
答案 3 :(得分:0)
// get app instance
UIApplication *app = [UIApplication sharedApplication];
// create local notif
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
NSDate *oneMinuteFromNow = [[NSDate date] dateByAddingTimeInterval:60];
notification.fireDate = oneMinuteFromNow;
notification.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage = @"First";
notification.alertBody = notificationMessage;
notification.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification];
// fire notification right away
[app presentLocalNotificationNow:notification];
}
UILocalNotification *notification1 = [[[UILocalNotification alloc] init] autorelease];
if (notification1) {
NSDate *oneMinuteFromNow1 = [[NSDate date] dateByAddingTimeInterval:120];
notification1.fireDate = oneMinuteFromNow1;
notification1.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage1 = @"Second";
notification1.alertBody = notificationMessage1;
notification1.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification1];
// fire notification right away
[app presentLocalNotificationNow:notification1];
}
通过写这个,你会在1分钟后得到一个通知,在2分钟后得到第二个通知。在“didReceiveLocalNotification
”方法中,您可以检查通知类型并显示警告消息。
希望这会对你有所帮助。