gpt4 book ai didi

iphone - 是否可以在应用程序中有两个本地通知

转载 作者:行者123 更新时间:2023-11-28 22:35:06 26 4
gpt4 key购买 nike

我想要两个本地通知,两者都有不同的时间,假设我的第一个通知将在 1 分钟后发出警报,第二个将在 2 分钟后发出警报。

我已经尝试在 appDelegate 中创建两个,但只有第一个给我通知,而不是第二个。

我怎样才能做到这一点?

最佳答案

是的,可以在任何 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 类中 Prepare Action 你想要的通知触发

//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

}

关于iphone - 是否可以在应用程序中有两个本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com