如何每天提供五次不同的五次通知?

时间:2013-07-09 21:32:30

标签: iphone ios objective-c uilocalnotification

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

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

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

这是我的代码;

编辑:我从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];

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

1 个答案:

答案 0 :(得分: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适用于此类事情)并避免为这些时间安排新通知