Scheduled local notification is not stored in schedLocalNotification array

Currently, I plan to receive local notifications once a day at 6 pm if the user has not yet opened the application that day. If the user has already downloaded the application, I want to cancel the notification for this day and plan a new one for tomorrow at 6 pm. The notification displays correctly, however, when I try to iterate through the list of scheduled notifications (this is not the only local notification that I have in the application), the [[UIApplication sharedApplication] scheduleLocalNotifications] array is always empty. The following is a snippet of code that is causing me problems:

// See if we need to create a local notification to tell the user that they can receive a daily reward NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty // Iterate over all the pending notifications for( int iter = 0; iter < [notifications count]; iter++ ) { UILocalNotification* localNotification = [notifications objectAtIndex:iter]; if( [[localNotification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; } NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents *nowComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:serverDate]; int hour = [nowComponents hour]; int minute = [nowComponents minute]; int second = [nowComponents second]; int hoursToAdd = 18 - hour; NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease]; [comps setDay:1]; [comps setHour:hoursToAdd]; [comps setMinute:-minute]; [comps setSecond:-second]; NSDate *tomorrow6PM = [calendar dateByAddingComponents:comps toDate:serverDate options:0]; NSMutableDictionary* userInfo = [NSMutableDictionary dictionary]; [userInfo setObject:@"dailyReminder" forKey:@"source"]; scheduleNotification(tomorrow6PM, NSLocalizedString(@"Come Back!", @"daily reminder notification message"), NSLocalizedString(@"Launch", @"daily reminder notification button text"), [UIApplication sharedApplication].applicationIconBadgeNumber+1, userInfo); 

The schedNotification function is simple and just creates a local notification:

 void scheduleNotification(NSDate* fireIn, NSString* bodyText, NSString* alertText, NSUInteger badgeCount, NSMutableDictionary* userInfo) { static int appCount = 0; appCount += 1; if(!isLocalNotificationEnabled()) return; Class localNotificationClass = NSClassFromString(@"UILocalNotification"); UILocalNotification* localNotification = [[localNotificationClass alloc] init]; if(!localNotification) return; localNotification.fireDate = fireIn; localNotification.timeZone = [NSTimeZone systemTimeZone]; localNotification.alertBody = bodyText; localNotification.alertAction = alertText; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.applicationIconBadgeNumber = badgeCount; localNotification.userInfo = userInfo; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; [localNotification release]; } 

I do not understand why the local notification array is always empty. As I said before displaying notifications at the right time, so they are planned. Any help is appreciated.

+2
source share
3 answers

Similar problems now. I assume that iOS does not send out notifications immediately, but only at the end of the current cycle of the cycle. I encounter these problems when setting the scheduleLocalNotifications property several times in the same run loop, and the changes do not seem to be updated accordingly. I think that I just keep a copy of the local notification array and put only schedLocalNotifications and never read it.

+7
source
 localNotification.fireDate = fireIn; 

on this line, verify that "fireIn" is an NSDate or NSString object.

If it NSString converts it to an NSDate using NSDateformatter , then returns it to localNotification fireDate.

I ran into the same problem earlier and resolved it with the above steps.

+1
source

If the schedule has an invalid fire date, it will not receive the planned one and will not return an empty array of results, because there will never be a single schedule, try printing a local value, as shown below, you may find this problem.

 NSLog(@"Notification--->: %@", localNotification); 
0
source

Source: https://habr.com/ru/post/975577/


All Articles