Before writing this post, I did a lot of research on the UserNotification Framework that replaced UILocalNotification in iOS 10. I also followed this guide to find out all about this new feature: http://useyourloaf.com/blog/local-notifications- with-ios-10 / .
Today I encounter such problems in order to implement such trivial notifications, and since this is a recent new feature, I could not find any solutions (especially in the C lens)! Currently, I have 2 different notifications, one Alert and one update icon .
Warning issue
Before updating my phone from iOS 10.1 to 10.2, I made a warning about Appdelegate, which starts immediately when the user closes the application manually:
-(void)applicationWillTerminate:(UIApplication *)application { NSLog(@"applicationWillTerminate"); // Notification terminate [self registerTerminateNotification]; } // Notification Background terminate -(void) registerTerminateNotification { // the center UNUserNotificationCenter * notifCenter = [UNUserNotificationCenter currentNotificationCenter]; // Content UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"Stop"; content.body = @"Application closed"; content.sound = [UNNotificationSound defaultSound]; // Trigger UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; // Identifier NSString *identifier = @"LocalNotificationTerminate"; // création de la requête UNNotificationRequest *terminateRequest = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; // Ajout de la requête au center [notifCenter addNotificationRequest:terminateRequest withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error %@: %@",identifier,error); } }]; }
Prior to iOS 10.2, it worked perfectly when I closed the application manually, a warning appeared. But since I upgraded to iOS 10.2, nothing appears without any reason, I can’t change anything, and I don’t see what is missing.
Icon Problem
I also tried (only on iOS 10.2 this time) to implement the icons on my application icon, which worked fine until I tried to remove it. Here is the function:
+(void) incrementBadgeIcon { // only increment if application is in background if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){ NSLog(@"increment badge"); // notif center UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter]; // Content UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.badge = [NSNumber numberWithInt:1]; // Trigger UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; // Identifier NSString *identifier = @"LocalNotificationIncrementBadge"; // request UNNotificationRequest *incrementBadgeRequest = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; // Ajout de la requête au center [notifCenter addNotificationRequest:incrementBadgeRequest withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error %@: %@",identifier,error); } }]; } }
Until it increments the icon number, as the name implies, but it just sets the icon number to 1. The documentation says that if you set content.badge to 0, it will remove it, but that will not work. I tried with other numbers, when I manually change it to "2", "3", etc ... it changes, but if I set it to 0, this will not work.
In addition, several functions were mentioned in the previous tutorial as getPendingNotificationRequests: completeHandler: and getDeliveredNotificationRequests: completeHandler:. I noticed that when I call these functions immediately after calling incrementBadgeIcon, if content.badge is set to "1", "2", etc., It appears in the list of pending notifications. However, when I set it to 0, it does not appear anywhere. I am not getting errors, no warnings in Xcode, and my application icon still remains.
Does anyone know how I can fix these two warnings?
thanks in advance
PS: I also tried using removeAllPendingNotificationRequests and removeAllDeliveredNotifications for both without success.