Remove iOS 9 local notification API usage

This commit is contained in:
SBiOSoftWhare 2019-04-25 11:52:33 +02:00
parent cd009f5fd1
commit d1d9b9f990

View file

@ -1159,7 +1159,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
if (!isPushRegistered) if (!isPushRegistered)
{ {
if (@available(iOS 10, *)) {
UNTextInputNotificationAction *quickReply = [UNTextInputNotificationAction UNTextInputNotificationAction *quickReply = [UNTextInputNotificationAction
actionWithIdentifier:@"inline-reply" actionWithIdentifier:@"inline-reply"
title:NSLocalizedStringFromTable(@"room_message_short_placeholder", @"Vector", nil) title:NSLocalizedStringFromTable(@"room_message_short_placeholder", @"Vector", nil)
@ -1191,29 +1190,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
} }
}]; }];
} }
else // DEPRECATED, for iOS 9
{
NSMutableSet *notificationCategories = [NSMutableSet set];
UIMutableUserNotificationAction *quickReply = [[UIMutableUserNotificationAction alloc] init];
quickReply.title = NSLocalizedStringFromTable(@"room_message_short_placeholder", @"Vector", nil);
quickReply.identifier = @"inline-reply";
quickReply.activationMode = UIUserNotificationActivationModeBackground;
quickReply.authenticationRequired = true;
quickReply.behavior = UIUserNotificationActionBehaviorTextInput;
UIMutableUserNotificationCategory *quickReplyCategory = [[UIMutableUserNotificationCategory alloc] init];
quickReplyCategory.identifier = @"QUICK_REPLY";
[quickReplyCategory setActions:@[quickReply] forContext:UIUserNotificationActionContextDefault];
[notificationCategories addObject:quickReplyCategory];
// Registration on iOS 8 and later
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:notificationCategories];
NSLog(@"[AppDelegate][Push] registerUserNotificationSettings: %@", settings);
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}
} }
- (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion - (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion
@ -1227,23 +1203,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
self.pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; self.pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
} }
// DEPRECATED, for iOS 9
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"[AppDelegate][Push] didRegisterUserNotificationSettings: notificationSettings.types: %@", @(notificationSettings.types));
// Register for remote notifications only if user provide access to notification feature
if (notificationSettings.types != UIUserNotificationTypeNone)
{
[self registerForRemoteNotificationsWithCompletion:nil];
}
else
{
// Clear existing token
[self clearPushNotificationToken];
}
}
// iOS 10+, see application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler: // iOS 10+, see application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{ {
@ -1297,38 +1256,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
} }
} }
// DEPRECATED, for iOS 9
// "This block is not a prototype" - don't fix this, or it won't match Apple's definition
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: identifier: %@", identifier);
NSString* roomId = notification.userInfo[@"room_id"];
if ([identifier isEqualToString: @"inline-reply"])
{
NSString* responseText = responseInfo[UIUserNotificationActionResponseTypedTextKey];
[self handleNotificationInlineReplyForRoomId:roomId withResponseText:responseText success:^(NSString *eventId) {
completionHandler();
} failure:^(NSError *error) {
UILocalNotification* failureNotification = [[UILocalNotification alloc] init];
failureNotification.alertBody = NSLocalizedStringFromTable(@"room_event_failed_to_send", @"Vector", nil);
failureNotification.userInfo = notification.userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification: failureNotification];
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: error sending text message: %@", error);
completionHandler();
}];
}
else
{
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: unhandled identifier %@", identifier);
completionHandler();
}
}
// iOS 10+, this is called when a notification is about to display in foreground. // iOS 10+, this is called when a notification is about to display in foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{ {
@ -1337,15 +1264,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
completionHandler(UNNotificationPresentationOptionNone); completionHandler(UNNotificationPresentationOptionNone);
} }
// DEPRECATED, for iOS 9
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"[AppDelegate][Push] didReceiveLocalNotification: applicationState: %@", @(application.applicationState));
NSString* roomId = notification.userInfo[@"room_id"];
[self navigateToRoomById:roomId];
}
- (void)navigateToRoomById:(NSString *)roomId - (void)navigateToRoomById:(NSString *)roomId
{ {
if (roomId.length) if (roomId.length)
@ -1522,15 +1440,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
BOOL checkReadEvent = YES; BOOL checkReadEvent = YES;
MXEvent *event; MXEvent *event;
// Ignore event already notified to the user
// only necessary on iOS 9, iOS 10 will just overwrite notifications with identical IDs
if (@available(iOS 10, *)) {}
else if ([self displayedLocalNotificationForEvent:eventId andUser:userId type:nil])
{
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Skip event already displayed in a notification. Event id: %@", eventId);
continue;
}
if (eventId && roomId) if (eventId && roomId)
{ {
event = [account.mxSession.store eventWithEventId:eventId inRoom:roomId]; event = [account.mxSession.store eventWithEventId:eventId inRoom:roomId];
@ -1539,17 +1448,9 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
if (event) if (event)
{ {
if (event.isRedactedEvent) if (event.isRedactedEvent)
{
if (@available(iOS 10, *))
{ {
// Collect redacted event ids to remove possible delivered redacted notifications // Collect redacted event ids to remove possible delivered redacted notifications
[redactedEventIds addObject:eventId]; [redactedEventIds addObject:eventId];
}
else
{
// Ignore redacted event.
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Skip redacted event. Event id: %@", eventId);
}
continue; continue;
} }
@ -1594,8 +1495,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
// Prepare the local notification // Prepare the local notification
MXPushRule *rule = eventDict[@"push_rule"]; MXPushRule *rule = eventDict[@"push_rule"];
if (@available(iOS 10, *))
{
[self notificationContentForEvent:event pushRule:rule inAccount:account onComplete:^(UNNotificationContent * _Nullable notificationContent) { [self notificationContentForEvent:event pushRule:rule inAccount:account onComplete:^(UNNotificationContent * _Nullable notificationContent) {
if (notificationContent) if (notificationContent)
@ -1624,43 +1523,8 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
} }
}]; }];
} }
else
{
[self notificationBodyForEvent:event pushRule:rule inAccount:account onComplete:^(NSString *_Nullable notificationBody)
{
if (notificationBody)
{
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Display notification for event %@", eventId);
// Printf style escape characters are stripped from the string prior to display;
// to include a percent symbol (%) in the message, use two percent symbols (%%).
NSString *fixedNotificationBody = [notificationBody stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
NSDictionary *notificationUserInfo = [self notificationUserInfoForEvent:event andUserId:userId];
NSString *categoryIdentifier = [self notificationCategoryIdentifierForEvent:event];
NSString *soundName = [self notificationSoundNameFromPushRule:rule];
UILocalNotification *eventNotification = [[UILocalNotification alloc] init];
eventNotification.alertBody = fixedNotificationBody;
eventNotification.userInfo = notificationUserInfo;
eventNotification.category = categoryIdentifier;
eventNotification.soundName = soundName;
[[UIApplication sharedApplication] scheduleLocalNotification:eventNotification];
scheduledNotifications++;
}
else
{
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Skip event with empty generated notificationBody. Event id: %@", eventId);
}
}];
}
}
} }
if (@available(iOS 10, *))
{
// Remove possible pending and delivered notifications having a redacted event id // Remove possible pending and delivered notifications having a redacted event id
if (redactedEventIds.count) if (redactedEventIds.count)
{ {
@ -1669,7 +1533,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:redactedEventIds]; [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:redactedEventIds];
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:redactedEventIds]; [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:redactedEventIds];
} }
}
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Sent %tu local notifications for %tu events", scheduledNotifications, eventsArray.count); NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Sent %tu local notifications for %tu events", scheduledNotifications, eventsArray.count);
@ -2059,15 +1922,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
for (NSString *eventId in events) for (NSString *eventId in events)
{ {
// Ignore event already notified to the user
// only necessary on iOS 9, iOS 10 will just overwrite notifications with identical IDs
if (@available(iOS 10, *)) {}
else if ([self displayedLocalNotificationForEvent:eventId andUser:userId type:nil])
{
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForAccount: Skip event already displayed in a notification. Event id: %@", eventId);
continue;
}
// Build notification user info // Build notification user info
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"limited", @"type": @"limited",
@ -2087,8 +1941,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForFailedSync: room_id is missing for event %@ in payload %@", eventId, payload); NSLog(@"[AppDelegate][Push] handleLocalNotificationsForFailedSync: room_id is missing for event %@ in payload %@", eventId, payload);
} }
if (@available(iOS 10, *))
{
UNMutableNotificationContent *localNotificationContentForFailedSync = [[UNMutableNotificationContent alloc] init]; UNMutableNotificationContent *localNotificationContentForFailedSync = [[UNMutableNotificationContent alloc] init];
localNotificationContentForFailedSync.userInfo = userInfo; localNotificationContentForFailedSync.userInfo = userInfo;
localNotificationContentForFailedSync.body = [self limitedNotificationBodyForEvent:eventId inMatrixSession:mxSession]; localNotificationContentForFailedSync.body = [self limitedNotificationBodyForEvent:eventId inMatrixSession:mxSession];
@ -2099,16 +1951,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForFailedSync: Display notification for event %@", eventId); NSLog(@"[AppDelegate][Push] handleLocalNotificationsForFailedSync: Display notification for event %@", eventId);
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
} }
else // DEPRECATED, for iOS 9
{
UILocalNotification *localNotificationForFailedSync = [[UILocalNotification alloc] init];
localNotificationForFailedSync.userInfo = userInfo;
localNotificationForFailedSync.alertBody = [self limitedNotificationBodyForEvent:eventId inMatrixSession:mxSession];
NSLog(@"[AppDelegate][Push] handleLocalNotificationsForFailedSync: Display notification for event %@", eventId);
[[UIApplication sharedApplication] scheduleLocalNotification:localNotificationForFailedSync];
}
}
} }
/** /**
@ -2147,39 +1989,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
return notificationBody; return notificationBody;
} }
/**
Return the already displayed notification for an event.
@param eventId the id of the event attached to the notification to find.
@param userId the id of the user attached to the notification to find.
@param type the type of notification. @"full" or @"limited". nil for any type.
@return the local notification if any.
*/
// DEPRECATED, for iOS 9
// TODO: This method does not work: [[UIApplication sharedApplication] scheduledLocalNotifications] is not reliable
- (UILocalNotification*)displayedLocalNotificationForEvent:(NSString*)eventId andUser:(NSString*)userId type:(NSString*)type
{
NSLog(@"[AppDelegate][Push] displayedLocalNotificationForEvent: %@ andUser: %@. Current scheduledLocalNotifications: %@", eventId, userId, [[UIApplication sharedApplication] scheduledLocalNotifications]);
UILocalNotification *limitedLocalNotification;
for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
NSLog(@" - %@", localNotification.userInfo);
if ([localNotification.userInfo[@"event_id"] isEqualToString:eventId]
&& [localNotification.userInfo[@"user_id"] isEqualToString:userId]
&& (!type || [localNotification.userInfo[@"type"] isEqualToString:type]))
{
limitedLocalNotification = localNotification;
break;
}
}
NSLog(@"[AppDelegate][Push] displayedLocalNotificationForEvent: found: %@", limitedLocalNotification);
return limitedLocalNotification;
}
- (void)refreshApplicationIconBadgeNumber - (void)refreshApplicationIconBadgeNumber
{ {
// Consider the total number of missed discussions including the invites. // Consider the total number of missed discussions including the invites.