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

标签 iphone ios objective-c xcode

我想要两个本地通知,两者都有不同的时间,假设我的第一个通知将在 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/

相关文章:

iphone - 有没有查条形码的网站?

swift - UIStoryBoard - 防止 View 重叠状态栏(iOS 11 应用程序)

ios - 如何将输入数据的文本字段传递到 swift 上的 url

iPhone - 根据文字调整UILabel宽度

objective-c - 将多个文件拖到沙盒应用程序

ios - 此应用无法通过在中国激活的 CallKit 功能获得批准。请进行适当的更改并重新提交此应用以供审核

ios - 如何检查以前打开的 View Controller

c++ - Objective C 到 C++ 桥的自动代码生成

ios - 重新启动应用程序后 iOS 上的随机数据丢失

iphone - 链接器命令失败,架构 i386 的符号未定义