简体   繁体   中英

How to fire multiple notification at the same time?

Can we fire multiple uilocalnotification say for 3 items at the same time for example: when the app is in background / foreground ,the notification is set to fire at 5:00 pm ..i should receive 3 notification should fire one after the other .It is only firing the notification which was set last. I have added the code .

    - (UILocalNotification *)scheduleNotification :(int)remedyID
    {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
        {
            NSString *descriptionBody;
            NSInteger frequency;
            UILocalNotification *notif = [[cls alloc] init];

            notif.timeZone = [NSTimeZone defaultTimeZone];
             for (int i=0; i<remedyArray.count; i++)
            {
                int arrayid = [[[remedyArray objectAtIndex:i] objectForKey:@"RemedyID"] intValue];
                if (arrayid == remedyID)
                {
                    descriptionBody=[[remedyArray objectAtIndex:i] objectForKey:@"RemedyTxtDic"];
                    frequency=[[[remedyArray objectAtIndex:i] objectForKey:@"RemedyFrequency"] integerValue];
                   break;
                }
            }
            NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
            for (NSDate *fireDate in notificationFireDates)      
  {
                Class cls = NSClassFromString(@"UILocalNotification");
                if (cls != nil)
                {

                    UILocalNotification *notif = [[cls alloc] init];                
                    notif.timeZone = [NSTimeZone defaultTimeZone];                
                    notif.repeatInterval = NSDayCalendarUnit;
                    notif.alertBody = [NSString stringWithString:descriptionBody];

                    notif.alertAction = @"Show me";
                    notif.soundName = UILocalNotificationDefaultSoundName;
                    notif.applicationIconBadgeNumber = 1;
                    notif.fireDate = fireDate;                

                    NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
    forKey:@"kRemindMeNotificationDataKey"];

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

                }
            }     

            return notif;
        }
        else
        {
            return nil;
        }

    }

If you're saying about calling your code like this:

   [something scheduleNotification:remedyOne];
   [something scheduleNotification:remedyTwo];
   [something scheduleNotification:remedyThree];

Then, rewrite your code. Because your code have

[[UIApplication sharedApplication] cancelAllLocalNotifications];

In first line of scheduleNotification: method.

So this is the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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