Multiple Local Notification in iPhone

2019-04-02 12:41发布

问题:

In my app, I have list of friends say: 3 friends, all these three have birthday details. I need to schedule local notification to show their b’days alert. I know and handled a local notification but how will I handle these multiple notification?

I am setting fire date in "for loop”.Is it proper, See the code.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
        return;
    NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
        NSString *birthday = [[myArray objectAtIndex:i]objectForKey:@"birthday"];

        [formatter setDateFormat:@"MM/dd/yyyy"];
        NSDate *date = [formatter dateFromString:birthday];

        localNotif.fireDate = [date dateByAddingTimeInterval:10];
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        NSLog(@"local %@",localNotif.fireDate);
    }

    localNotif.applicationIconBadgeNumber = 1;

    NSString *itemName = @“Friend Name";
    NSDictionary *userDict = [NSDictionary dictionaryWithObjectAndKey:itemName,@"msg", nil];

    localNotif.userInfo = userDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

What I want,in this method only I have to set notification for all the friend based on their date. can any one tell me where I’m doing wrong and if I’m missing anything please inform me.

回答1:

Just make three (or more) local notifications and schedule every one of them with scheduleLocalNotification:, what's the problem? For example this is what I did in my project:

for (int i = 0; i < 6; i++) {
    UILocalNotification *localNotification = [prototypeNotification copy];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:dates];
    [notifications addObject:localNotification];


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    [localNotification release];
}

UPD

// ...this goes earlier:
static NotificationController *sharedNotificationController = nil;

- (id) init 
{
    if (self = [super init]) {
        notifications = [[NSMutableArray alloc] init];

        prototypeNotification = [[UILocalNotification alloc] init];
        prototypeNotification.repeatCalendar = [NSCalendar currentCalendar];
        prototypeNotification.repeatInterval = NSMinuteCalendarUnit;

        prototypeNotification.timeZone = [NSTimeZone defaultTimeZone];
        prototypeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        prototypeNotification.applicationIconBadgeNumber = 0;
        prototypeNotification.alertBody = NSLocalizedString(@"Body", nil);
        prototypeNotification.alertAction = NSLocalizedString(@"Action", nil);

        enabled_ = NO;
    }
    return self;
}