Merge branch 'charlesmchen/deferNotifications'

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

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

@ -66,7 +66,10 @@ typedef void (^pushTokensSuccessBlock)(NSString *pushToken, NSString *voipToken)
- (TOCFuture *)registerPushKitNotificationFuture;
- (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;
#pragma mark Push Notifications Delegate Methods

@ -163,7 +163,7 @@
failedSendNotif.alertBody =
[NSString stringWithFormat:NSLocalizedString(@"NOTIFICATION_SEND_FAILED", nil), [thread name]];
failedSendNotif.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
[self presentNotification:failedSendNotif];
[self presentNotification:failedSendNotif checkForCancel:NO];
completionHandler();
}];
}
@ -429,18 +429,32 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
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];
[self.currentNotifications addObject:notification];
}
- (void)cancelNotificationsWithThreadId:(NSString *)threadId {
OWSAssert([NSThread isMainThread]);
NSMutableArray *toDelete = [NSMutableArray array];
[self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
[toDelete addObject:notif];
}
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
[toDelete addObject:notif];
}
}];
[self.currentNotifications removeObjectsInArray:toDelete];
}

Loading…
Cancel
Save