How do I know which fireDate is in the next scheduled notification?

Asked

Viewed 54 times

0

I wonder if there’s a way to catch the next fireDate from the current date.

I have a method that creates the notification and repeats it every given period. How do I catch the next fireDate? I would need that to display in a string.

- (void)criarNotificacao {
    NSDate *novaData = self.data;
    for (int i = 0; i < (24/self.intervalo); i++) {
        UILocalNotification *notificacao = [[UILocalNotification alloc] init];
        notificacao.fireDate = novaData;
        notificacao.alertBody = self.nome;
        notificacao.soundName = UILocalNotificationDefaultSoundName;
        notificacao.timeZone = [NSTimeZone defaultTimeZone];
        notificacao.repeatInterval = NSCalendarUnitDay;
        notificacao.applicationIconBadgeNumber = 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:notificacao];
        novaData = [NSDate dateWithTimeInterval:(self.intervalo*3600) sinceDate:novaData];
    }
}

1 answer

1

The class UIApplication owns the property scheduledLocalNotifications, which has a list of all scheduled notifications.

You might have something like this:

UIApplication *application = [UIApplication sharedApplication];
NSArray *notifications = [application scheduledLocalNotifications];

And on top of this array, get the next notification based on its date, resulting in an object UILocalNotification.

NSSortDescriptor *fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];
NSArray *sorted = [notifications sortedArrayUsingDescriptors:@[fireDateDesc]]
UILocalNotification *nextNotification =  [sorted objectAtIndex:0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.