0

我正在开发一个伊斯兰祈祷时间应用程序,该应用程序每天提供五个祈祷时间。所以我现在的目标是每天在特定的祈祷时间向用户发送五个通知。然后我为每个祈祷创建了五个通知,为每个通知分配了五个 fireate,我可以收到通知。

但是,我的问题是每次运行应用程序或重新启动应用程序时,我都会在通知中心收到通知,在控制台区域我可以看到所有较旧的通知也已发送(我覆盖了 application:didReceiveLocalNotification: 方法)。

坦率地说,我不是一个经验丰富的开发人员,我真的不明白这一点,我认为我的代码很长。那么有人可以帮助我并告诉我如何做到这一点吗?:) (我的英语不是那么好请宽容)。如果我错过了其他歌曲来告知,请告诉。

这是我的代码;

编辑:我从 viewDidLoad 调用这个方法。

我在阵列中获得了五次打击:

 NSSArray *timeArray = @[time0,time1,time2,time3,time4];

我像这样安排了五个 UILocalNotification:

   if (localNotification0 != nil) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification0];
}
NSDate *date0 = [_timeArray objectAtIndex:0];
NSLog(@"date %@",date0);
localNotification0 = [[UILocalNotification alloc] init];
localNotification0.fireDate = date0;
localNotification0.timeZone =[NSTimeZone defaultTimeZone];
NSLog(@"firedate %@",localNotification0.timeZone);
localNotification0.alertBody = @"Se";
localNotification0.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification0];


if (localNotification1 != nil) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification1];
}
NSDate *date1 = [_timeArray objectAtIndex:1];
localNotification1 = [[UILocalNotification alloc] init];
localNotification1.fireDate = date1;
NSLog(@"firedate %@",localNotification1.fireDate);
localNotification1.timeZone =[NSTimeZone defaultTimeZone];
localNotification1.alertBody = @"Se";
localNotification1.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification1];
[localNotification1 release];

if (localNotification2 != nil) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification2];
}
NSDate *date2 = [_timeArray objectAtIndex:2];
localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = date2;
localNotification2.timeZone =[NSTimeZone defaultTimeZone];
NSLog(@"firedate %@",localNotification2.fireDate);

localNotification2.alertBody = @"Se";
localNotification2.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
[localNotification2 release];


if (localNotification3 != nil) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification3];
}
NSDate *date3 = [_timeArray objectAtIndex:3];
localNotification3 = [[UILocalNotification alloc] init];
localNotification3.fireDate = date3;
localNotification3.timeZone =[NSTimeZone defaultTimeZone];
NSLog(@"firedate %@",localNotification3.fireDate);

localNotification3.alertBody = @"Se";
localNotification3.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification3];
[localNotification3 release];

if (localNotification4 != nil) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotification4];
}
NSDate *date4 = [_timeArray objectAtIndex:4];
localNotification4 = [[UILocalNotification alloc] init];
localNotification4.fireDate = date4;
localNotification4.timeZone =[NSTimeZone defaultTimeZone];
NSLog(@"firedate %@",localNotification4.fireDate);

localNotification4.alertBody = @"Se";
localNotification4.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification4];
[localNotification4 release];

还有其他更简单的方法吗?请帮帮我!

4

1 回答 1

0

对于初学者,看起来您可以通过使用循环来缩短代码,如下所示:

for (NSDate *time in times) {
    if (localNotification3 != nil) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification3];
    }
    UILocalNotification *note = [[UILocalNotification alloc] init];
    note.fireDate = time;
    note.timeZone =[NSTimeZone defaultTimeZone];
    note.alertBody = @"Se";
    note.soundName = _adhanName;
    [[UIApplication sharedApplication] scheduleLocalNotification:note];
    [note release]; // no need to release note if you use ARC
}

该循环将为数组中的每个条目运行一次,在这种情况下或一般times情况下将所需的代码量减少 5 倍。[times count]

每次我运行应用程序或重新启动应用程序时,我都会在通知中心收到通知

我不确定我是否完全理解这里出了什么问题,但如果任何/所有通知都过早呈现,听起来你应该检查它们的安排时间。如果问题是您的应用程序每次启动时都会不断安排新的通知,从而导致通知过多,那么您至少有两种选择:

  • -cancelAllLocalNotifications在安排任何新通知之前使用

  • 记录已经安排通知的时间(NSUserDefaults 适合这种事情)并避免在这些时间安排新通知

于 2013-07-09T23:29:22.940 回答