1

I am using local notifications in my app to alert urgent messages to the user. What happens is the user receives a push notification, then a local notification is created and fired 60 seconds later with a time interval of 60 seconds. This works great and the urgent notification fires every 60 seconds as expected.

The problem is after about 20 minutes the local notification stops firing. This does not occur all the time and sometimes the notification can fire for hours with no problem. However, this problem seems to occur more often than not.

On iOS 9 we did not experience this problem at all and the notification would fire repeatedly overnight even, so I am thinking this might be something related to iOS 10?

I've also got a hunch that perhaps this has to do with memory pressure from the OS? If I attach to xCode and debug then the problem does not occur at all and will fire into infinity.

The code I use to create the notification is as follows:

[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
    dispatch_async(dispatch_get_main_queue(), ^{

        //If notification already exists then kill it with fire
        [self removeAllLocalNotifications];

        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

        content.title = @"Urgent";
        content.sound = [UNNotificationSound soundNamed:@"dingdingding.wav"];
        content.categoryIdentifier = @"urgentCategoryId";
        content.body = @"You have an urgent message.";

        // Deliver the notification in scheduled amount of seconds.
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"urgentMessageId" content:content trigger:trigger];

        // Schedule the notification.
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error) {
                    NSLog(@"Notification creation Error");
                }

                if (completionHandler) {
                    completionHandler(UIBackgroundFetchResultNewData);
                }
            });
        }];
    });
}];

Any help would be much appreciated as this is a very frustrating issue.

Nick Kirsten
  • 1,187
  • 11
  • 27

0 Answers0