Respond to CR.

// FREEBIE
pull/1/head
Matthew Chen 9 years ago
parent b7b5dbb563
commit 2a369273c8

@ -23,7 +23,6 @@
@property TOCFutureSource *registerWithServerFutureSource; @property TOCFutureSource *registerWithServerFutureSource;
@property UIAlertView *missingPermissionsAlertView; @property UIAlertView *missingPermissionsAlertView;
@property (nonatomic, retain) NSMutableArray *currentNotifications; @property (nonatomic, retain) NSMutableArray *currentNotifications;
@property (nonatomic, retain) NSMutableArray *pendingNotifications;
@property (nonatomic) UIBackgroundTaskIdentifier callBackgroundTask; @property (nonatomic) UIBackgroundTaskIdentifier callBackgroundTask;
@property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) OWSMessageFetcherJob *messageFetcherJob; @property (nonatomic, readonly) OWSMessageFetcherJob *messageFetcherJob;
@ -76,8 +75,7 @@
cancelButtonTitle:NSLocalizedString(@"OK", @"") cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
_callBackgroundTask = UIBackgroundTaskInvalid; _callBackgroundTask = UIBackgroundTaskInvalid;
_currentNotifications = [NSMutableArray new]; _currentNotifications = [NSMutableArray array];
_pendingNotifications = [NSMutableArray new];
OWSSingletonAssert(); OWSSingletonAssert();
@ -437,55 +435,27 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key]; NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
if (checkForCancel && threadId != nil) { if (checkForCancel && threadId != nil) {
[_pendingNotifications addObject:notification];
// 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;
dispatch_after( notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds];
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC * kDelaySeconds)), dispatch_get_main_queue(), ^{ notification.timeZone = [NSTimeZone localTimeZone];
if (![_pendingNotifications containsObject:notification]) {
DDLogVerbose(@"%@ notification was cancelled before it was presented: %@, %@",
self.tag,
notification.alertBody,
threadId);
} else {
[_pendingNotifications removeObject:notification];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification];
}
});
} else {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification];
} }
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification];
} }
- (void)cancelNotificationsWithThreadId:(NSString *)threadId { - (void)cancelNotificationsWithThreadId:(NSString *)threadId {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
// Cull matching pending notifications.
NSMutableArray *toDelete = [NSMutableArray array]; NSMutableArray *toDelete = [NSMutableArray array];
[self.pendingNotifications enumerateObjectsUsingBlock:^( [self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
UILocalNotification *notification, NSUInteger idx, BOOL *stop) { if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
NSString *notificationThreadId = notification.userInfo[Signal_Thread_UserInfo_Key]; [[UIApplication sharedApplication] cancelLocalNotification:notif];
OWSAssert(notificationThreadId != nil); [toDelete addObject:notif];
if ([notificationThreadId isEqualToString:threadId]) {
DDLogError(@"%@ cancelling delayed notification: %@, %@", self.tag, notification.alertBody, threadId);
[toDelete addObject:notification];
} }
}]; }];
[self.pendingNotifications removeObjectsInArray:toDelete];
// Cull matching active notifications.
[toDelete removeAllObjects];
[self.currentNotifications
enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
if ([notification.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[toDelete addObject:notification];
}
}];
[self.currentNotifications removeObjectsInArray:toDelete]; [self.currentNotifications removeObjectsInArray:toDelete];
} }

Loading…
Cancel
Save