如何在ios中管理多个本地通知?

时间:2013-11-28 10:10:59

标签: ios iphone notifications

我有一个多个本地通知,每隔几分钟重复一次。当本地通知到达时,我更新数据库。它工作正常。但是当我们设置多个通知时,它的触发时间是相同的。因此两个本地通知同时同时执行。* 这种情况进入我的数据库更新无法正常工作。意味着第一个值参考数据已更新 < / strong> *

//下面设置通知。

   AlarmNotification = [[UILocalNotification alloc]init];
   // AlarmNotification.fireDate = SetAlarmTime;
    AlarmNotification.fireDate = AddMinutes;
    AlarmNotification.repeatInterval = NSMinuteCalendarUnit; 
    AlarmNotification.alertBody =cloclListInsert.labelText;
    AlarmNotification.timeZone  = [NSTimeZone defaultTimeZone];
    AlarmNotification.soundName =cloclListInsert.SelctAlaemToneDbStr;


    NSMutableDictionary *userDict = [[NSMutableDictionary alloc] init];
    NSString *ClockIDStr = [NSString stringWithFormat:@"%i",cloclListInsert.ClockIDvalue];

    [userDict setObject:ClockIDStr forKey:@"ClockID"];

    AlarmNotification.userInfo = userDict;
    [[UIApplication sharedApplication]scheduleLocalNotification:AlarmNotification];

App Delegate中的委托方法

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
 {
    NSLog(@"notification.userInfo : %@",notification.userInfo);

    NSString *SelectID= [notification.userInfo objectForKey:@"ClockID"];
    NSLog(@"SelectID is : %@",SelectID);

    NSLog(@"notification.userInfo : %@",notification.userInfo);
    NotificationId = [SelectID integerValue];


    NSLog(@"NotificationId is : %i",NotificationId);
    clocklistobj.ClockIDvalue = NotificationId;

    if ([sqldbobj getNotificationClockList:clocklistobj]) {

        NotificationCount = clocklistobj.incrementTag;

        NSLog(@"NotificationCount : %i",NotificationCount);

        clocklistobj.incrementTag =NotificationCount +1;

        //insert increment tag in counter variable

        counter = clocklistobj.incrementTag;
        NSLog(@"counter when receive notification in delegate method: %i",counter);

        NSLog(@"NotificationCount +1 & clocklistobj.incrementTag : %i",clocklistobj.incrementTag);

        //set brightness of device
        SetBrightnessValue = (float) (clocklistobj.incrementTag *0.066666);   
        NSLog(@"SetBrightnessValue : %f",SetBrightnessValue);
        [[UIScreen mainScreen] setBrightness:SetBrightnessValue];

        //set music player & control volume

        setPlayerVolume = (float) (clocklistobj.incrementTag *0.066666);

        NSLog(@"set Player Volume : %f",setPlayerVolume);

        [audioPlayer setVolume:setPlayerVolume];

        //below start playing audio

        [audioPlayer play];

        //update database

        [sqldbobj UpdateNotificationCount:clocklistobj];
        NSLog(@" After update clocklistobj.incrementTag:%i",clocklistobj.incrementTag);

        if (clocklistobj.incrementTag ==15 || clocklistobj.incrementTag >15) {
            NSLog(@"Notification reach at limit");
            UIApplication *app = [UIApplication sharedApplication];
            NSArray *eventArray = [app scheduledLocalNotifications];

            // NSLog(@"eventArray : %@",eventArray);

            for (int i=0; i<[eventArray count]; i++)
            {
                UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
                NSDictionary *userInfoCurrent = oneEvent.userInfo;
            NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"ClockID"]];
                NSInteger GetUid = [uid integerValue];

                //here we cancell the particular notification
            NSLog(@"Application is stoped clocklistobj.ClockIDvalue : %i", clocklistobj.ClockIDvalue);

                if (GetUid ==clocklistobj.ClockIDvalue)
                {
                    clocklistobj.soundisON = 0;
                    [sqldbobj UpdateAfteNotificationSwitchMakeOff:clocklistobj];

                    //Cancelling local notification
                    [app cancelLocalNotification:oneEvent];
                    break;

                }
            }
    }else {
        NSLog(@"Notification is off");
    }
    }
 }

1 个答案:

答案 0 :(得分:0)

好的,你可以做的是限制用户同时设置多个闹钟。

为此,您必须致电[[UIApplication sharedApplication] scheduledLocalNotifications],它将为您提供迄今为止安排的所有通知。

然后您可以查看这些通知,并检查notification.fireDate是否与您当前的日程安排fireDate匹配,然后您可以通过说“同时安排另一个警报”来向用户显示警报。