Merge branch 'charlesmchen/deferNotifications'

pull/1/head
Matthew Chen 8 years ago
commit bf02bd2cc8

@ -136,7 +136,7 @@
} }
notification.alertBody = alertBodyString; notification.alertBody = alertBodyString;
[[PushManager sharedManager] presentNotification:notification]; [[PushManager sharedManager] presentNotification:notification checkForCancel:NO];
} else { } else {
if ([Environment.preferences soundInForeground]) { if ([Environment.preferences soundInForeground]) {
AudioServicesPlayAlertSound(_newMessageSound); AudioServicesPlayAlertSound(_newMessageSound);
@ -193,7 +193,7 @@
break; break;
} }
[[PushManager sharedManager] presentNotification:notification]; [[PushManager sharedManager] presentNotification:notification checkForCancel:YES];
} else { } else {
if ([Environment.preferences soundInForeground]) { if ([Environment.preferences soundInForeground]) {
AudioServicesPlayAlertSound(_newMessageSound); AudioServicesPlayAlertSound(_newMessageSound);

@ -66,7 +66,10 @@ typedef void (^pushTokensSuccessBlock)(NSString *pushToken, NSString *voipToken)
- (TOCFuture *)registerPushKitNotificationFuture; - (TOCFuture *)registerPushKitNotificationFuture;
- (BOOL)supportsVOIPPush; - (BOOL)supportsVOIPPush;
- (void)presentNotification:(UILocalNotification *)notification; // If checkForCancel is set, the notification will be delayed for
// a moment. If a relevant cancel notification is received in that window,
// the notification will not be displayed.
- (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel;
- (void)cancelNotificationsWithThreadId:(NSString *)threadId; - (void)cancelNotificationsWithThreadId:(NSString *)threadId;
#pragma mark Push Notifications Delegate Methods #pragma mark Push Notifications Delegate Methods

@ -163,7 +163,7 @@
failedSendNotif.alertBody = failedSendNotif.alertBody =
[NSString stringWithFormat:NSLocalizedString(@"NOTIFICATION_SEND_FAILED", nil), [thread name]]; [NSString stringWithFormat:NSLocalizedString(@"NOTIFICATION_SEND_FAILED", nil), [thread name]];
failedSendNotif.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId }; failedSendNotif.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
[self presentNotification:failedSendNotif]; [self presentNotification:failedSendNotif checkForCancel:NO];
completionHandler(); completionHandler();
}]; }];
} }
@ -429,18 +429,32 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
return NO; return NO;
} }
- (void)presentNotification:(UILocalNotification *)notification { - (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel
{
OWSAssert([NSThread isMainThread]);
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
if (checkForCancel && threadId != nil) {
// The longer we wait, the more obsolete notifications we can suppress -
// but the more lag we introduce to notification delivery.
const CGFloat kDelaySeconds = 0.3f;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds];
notification.timeZone = [NSTimeZone localTimeZone];
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification]; [self.currentNotifications addObject:notification];
} }
- (void)cancelNotificationsWithThreadId:(NSString *)threadId { - (void)cancelNotificationsWithThreadId:(NSString *)threadId {
OWSAssert([NSThread isMainThread]);
NSMutableArray *toDelete = [NSMutableArray array]; NSMutableArray *toDelete = [NSMutableArray array];
[self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) { [self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) { if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif]; [[UIApplication sharedApplication] cancelLocalNotification:notif];
[toDelete addObject:notif]; [toDelete addObject:notif];
} }
}]; }];
[self.currentNotifications removeObjectsInArray:toDelete]; [self.currentNotifications removeObjectsInArray:toDelete];
} }

Loading…
Cancel
Save