简体   繁体   English

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

[英]how to manage multiple Local notification in ios?

I have a multiple local notification which repeat every minutes.when local notification are arrive then i update the database.It's Work fine. 我有多个本地通知,每分钟重复一次。到达本地通知后,我将更新数据库。 But when we set multiple notification then It's fire time is Same.so both local notification simultaneously execute at same time.* This situation into my database update is not work properly.means first value reference data are updated * 但是,当我们设置多个通知时,它的触发时间是相同的。因此,两个本地通知同时执行。* 这种情况无法正常更新到我的数据库中。意味着第一个值参考数据已更新 *

//below set notification. //在设置通知下方。

   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];

delegate method in App Delegate 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");
    }
    }
 }

Ok so what you can do is you can restrict the user from setting multiple alarm at the same time. 好的,所以您可以做的是限制用户同时设置多个警报。

For doing so you have to call [[UIApplication sharedApplication] scheduledLocalNotifications] , it will provide you all the notifications scheduled till now. 为此,您必须调用[[UIApplication sharedApplication] scheduledLocalNotifications] ,它将为您提供[[UIApplication sharedApplication] scheduledLocalNotifications]所有通知。

Then you can go through these notification and check for the notification.fireDate if it matches your current scheduling fireDate then you can show an alert to user by saying that 'There is another alarm scheduled at the same time.'. 然后,您可以查看这些通知并检查notification.fireDate如果它与您当前的计划fireDate相匹配,则可以通过说“同时计划另一个警报”来向用户显示警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM