Merge branch 'charlesmchen/notificationsVsConcurrency'

pull/1/head
Matthew Chen 8 years ago
commit 2c46220ebf

@ -211,31 +211,33 @@
- (void)presentNotification:(UILocalNotification *)notification identifier:(NSString *)identifier - (void)presentNotification:(UILocalNotification *)notification identifier:(NSString *)identifier
{ {
AssertIsOnMainThread(); dispatch_async(dispatch_get_main_queue(), ^{
// Replace any existing notification
// Replace any existing notification // e.g. when an "Incoming Call" notification gets replaced with a "Missed Call" notification.
// e.g. when an "Incoming Call" notification gets replaced with a "Missed Call" notification. if (self.currentNotifications[identifier]) {
if (self.currentNotifications[identifier]) { [self cancelNotificationWithIdentifier:identifier];
[self cancelNotificationWithIdentifier:identifier]; }
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
DDLogDebug(@"%@ presenting notification with identifier: %@", self.tag, identifier); DDLogDebug(@"%@ presenting notification with identifier: %@", self.tag, identifier);
self.currentNotifications[identifier] = notification; self.currentNotifications[identifier] = notification;
});
} }
- (void)cancelNotificationWithIdentifier:(NSString *)identifier - (void)cancelNotificationWithIdentifier:(NSString *)identifier
{ {
AssertIsOnMainThread(); dispatch_async(dispatch_get_main_queue(), ^{
UILocalNotification *notification = self.currentNotifications[identifier]; UILocalNotification *notification = self.currentNotifications[identifier];
if (!notification) { if (!notification) {
DDLogWarn(@"%@ Couldn't cancel notification because none was found with identifier: %@", self.tag, identifier); DDLogWarn(
return; @"%@ Couldn't cancel notification because none was found with identifier: %@", self.tag, identifier);
} return;
[self.currentNotifications removeObjectForKey:identifier]; }
[self.currentNotifications removeObjectForKey:identifier];
[[UIApplication sharedApplication] cancelLocalNotification:notification]; [[UIApplication sharedApplication] cancelLocalNotification:notification];
});
} }
#pragma mark - Logging #pragma mark - Logging

@ -431,32 +431,34 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
- (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel - (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel
{ {
OWSAssert([NSThread isMainThread]); dispatch_async(dispatch_get_main_queue(), ^{
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key]; if (checkForCancel && threadId != nil) {
if (checkForCancel && threadId != nil) { // The longer we wait, the more obsolete notifications we can suppress -
// The longer we wait, the more obsolete notifications we can suppress - // but the more lag we introduce to notification delivery.
// but the more lag we introduce to notification delivery. const CGFloat kDelaySeconds = 0.3f;
const CGFloat kDelaySeconds = 0.3f; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds]; notification.timeZone = [NSTimeZone localTimeZone];
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]); {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *toDelete = [NSMutableArray array]; NSMutableArray *toDelete = [NSMutableArray array];
[self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) { [self.currentNotifications
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) { enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
[[UIApplication sharedApplication] cancelLocalNotification:notif]; if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[toDelete addObject:notif]; [[UIApplication sharedApplication] cancelLocalNotification:notif];
} [toDelete addObject:notif];
}]; }
[self.currentNotifications removeObjectsInArray:toDelete]; }];
[self.currentNotifications removeObjectsInArray:toDelete];
});
} }
+ (NSString *)tag + (NSString *)tag

Loading…
Cancel
Save