Reliability enhancements for notifications.

pull/1/head
Frederic Jacobs 10 years ago
parent 0f57804eef
commit 0f45294221

@ -145,7 +145,7 @@
} }
-(void)applicationDidBecomeActive:(UIApplication *)application { -(void)applicationDidBecomeActive:(UIApplication *)application {
if ([TSAccountManager isRegistered] && [self applicationIsActive]) { if ([TSAccountManager isRegistered]) {
// We're double checking that the app is active, to be sure since we can't verify in production env due to code signing. // We're double checking that the app is active, to be sure since we can't verify in production env due to code signing.
[TSSocketManager becomeActiveFromForeground]; [TSSocketManager becomeActiveFromForeground];
} }
@ -159,6 +159,7 @@
if ([TSAccountManager isRegistered]) { if ([TSAccountManager isRegistered]) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[self updateBadge]; [self updateBadge];
[TSSocketManager resignActivity];
} }
} }
@ -168,10 +169,6 @@
} }
} }
- (void)applicationDidEnterBackground:(UIApplication *)application{
[TSSocketManager resignActivity];
}
- (void)prepareScreenshotProtection{ - (void)prepareScreenshotProtection{
self.blankWindow = ({ self.blankWindow = ({
UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds]; UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds];

@ -416,6 +416,7 @@
UILocalNotification *notification = [[UILocalNotification alloc] init]; UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.category = Signal_Message_Category; notification.category = Signal_Message_Category;
notification.userInfo = @{Signal_Thread_UserInfo_Key:thread.uniqueId}; notification.userInfo = @{Signal_Thread_UserInfo_Key:thread.uniqueId};
notification.soundName = @"NewMessage.aifc";
switch ([[Environment preferences] notificationPreviewType]) { switch ([[Environment preferences] notificationPreviewType]) {
case NotificationNamePreview: case NotificationNamePreview:
@ -447,7 +448,6 @@
} }
[[UIApplication sharedApplication] presentLocalNotificationNow:notification]; [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
AudioServicesPlayAlertSound(_newMessageSound);
} else { } else {
if ([Environment.preferences soundInForeground]) { if ([Environment.preferences soundInForeground]) {
AudioServicesPlayAlertSound(_newMessageSound); AudioServicesPlayAlertSound(_newMessageSound);

@ -22,6 +22,7 @@
#define kWebSocketHeartBeat 30 #define kWebSocketHeartBeat 30
#define kWebSocketReconnectTry 5 #define kWebSocketReconnectTry 5
#define kBackgroundConnectTimer 120 #define kBackgroundConnectTimer 120
#define kBackgroundConnectKeepAlive 20
NSString * const SocketOpenedNotification = @"SocketOpenedNotification"; NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
NSString * const SocketClosedNotification = @"SocketClosedNotification"; NSString * const SocketClosedNotification = @"SocketClosedNotification";
@ -30,11 +31,11 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
@interface TSSocketManager () @interface TSSocketManager ()
@property (nonatomic, retain) NSTimer *pingTimer; @property (nonatomic, retain) NSTimer *pingTimer;
@property (nonatomic, retain) NSTimer *reconnectTimer; @property (nonatomic, retain) NSTimer *reconnectTimer;
@property (nonatomic, retain) NSTimer *backgroundKeepAliveTimer;
@property (nonatomic, retain) SRWebSocket *websocket; @property (nonatomic, retain) SRWebSocket *websocket;
@property (nonatomic) SocketStatus status; @property (nonatomic) SocketStatus status;
@property (nonatomic, retain) NSTimer *backgroundConnectTimer;
@property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier; @property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier;
@property BOOL didFetchInBackground; @property BOOL didFetchInBackground;
@ -59,6 +60,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init]; sharedMyManager = [[self alloc] init];
sharedMyManager.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
sharedMyManager.didFetchInBackground = FALSE;
}); });
return sharedMyManager; return sharedMyManager;
} }
@ -118,6 +121,11 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
selector:@selector(webSocketHeartBeat) selector:@selector(webSocketHeartBeat)
userInfo:nil userInfo:nil
repeats:YES]; repeats:YES];
// Additionally, we want the ping timer to work in the background too.
[[NSRunLoop mainRunLoop] addTimer:self.pingTimer
forMode:NSDefaultRunLoopMode];
self.status = kSocketStatusOpen; self.status = kSocketStatusOpen;
[self.reconnectTimer invalidate]; [self.reconnectTimer invalidate];
@ -149,6 +157,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
[self sendWebSocketMessageAcknowledgement:message]; [self sendWebSocketMessageAcknowledgement:message];
[self keepAliveBackground];
if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]){ if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]){
NSData *decryptedPayload = [Cryptography decryptAppleMessagePayload:message.body NSData *decryptedPayload = [Cryptography decryptAppleMessagePayload:message.body
withSignalingKey:TSStorageManager.signalingKey]; withSignalingKey:TSStorageManager.signalingKey];
@ -166,6 +176,22 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
} }
} }
- (void)keepAliveBackground {
if (self.fetchingTaskIdentifier) {
[self.backgroundKeepAliveTimer invalidate];
self.backgroundKeepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:kBackgroundConnectKeepAlive
target:self
selector:@selector(backgroundTimeExpired)
userInfo:nil
repeats:NO];
// Additionally, we want the reconnect timer to work in the background too.
[[NSRunLoop mainRunLoop] addTimer:self.backgroundKeepAliveTimer
forMode:NSDefaultRunLoopMode];
}
}
- (void)processWebSocketResponseMessage:(WebSocketResponseMessage*)message { - (void)processWebSocketResponseMessage:(WebSocketResponseMessage*)message {
DDLogWarn(@"Client should not receive WebSocket Respond messages"); DDLogWarn(@"Client should not receive WebSocket Respond messages");
} }
@ -215,6 +241,9 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
selector:@selector(becomeActive) selector:@selector(becomeActive)
userInfo:nil userInfo:nil
repeats:YES]; repeats:YES];
// Additionally, we want the reconnect timer to work in the background too.
[[NSRunLoop mainRunLoop] addTimer:self.reconnectTimer
forMode:NSDefaultRunLoopMode];
} }
} }
@ -222,10 +251,11 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
+ (void)becomeActiveFromForeground { + (void)becomeActiveFromForeground {
TSSocketManager *sharedInstance = [self sharedManager]; TSSocketManager *sharedInstance = [self sharedManager];
[sharedInstance.backgroundKeepAliveTimer invalidate];
[sharedInstance.backgroundConnectTimer invalidate]; if (sharedInstance.fetchingTaskIdentifier != UIBackgroundTaskInvalid) {
sharedInstance.backgroundConnectTimer = nil; [sharedInstance closeBackgroundTask];
sharedInstance.fetchingTaskIdentifier = 0; }
[self becomeActive]; [self becomeActive];
} }
@ -234,25 +264,34 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
+ (void)becomeActiveFromBackground { + (void)becomeActiveFromBackground {
TSSocketManager *sharedInstance = [TSSocketManager sharedManager]; TSSocketManager *sharedInstance = [TSSocketManager sharedManager];
if (sharedInstance.fetchingTaskIdentifier == 0) { if (sharedInstance.fetchingTaskIdentifier == UIBackgroundTaskInvalid) {
[sharedInstance.backgroundKeepAliveTimer invalidate];
sharedInstance.didFetchInBackground = NO; sharedInstance.didFetchInBackground = NO;
sharedInstance.fetchingTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ sharedInstance.fetchingTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
if (!sharedInstance.didFetchInBackground) {
[sharedInstance backgroundConnectTimedOut];
}
sharedInstance.fetchingTaskIdentifier = 0;
[TSSocketManager resignActivity]; [TSSocketManager resignActivity];
[sharedInstance closeBackgroundTask];
}]; }];
[self becomeActive]; [self becomeActive];
} else {
DDLogWarn(@"Got called to become active in the background but there was already a background task running.");
} }
} }
- (void)backgroundTimeExpired {
[[self class] resignActivity];
[self closeBackgroundTask];
}
- (void)closeBackgroundTask { - (void)closeBackgroundTask {
UIBackgroundTaskIdentifier identifier = self.fetchingTaskIdentifier; UIBackgroundTaskIdentifier identifier = self.fetchingTaskIdentifier;
self.fetchingTaskIdentifier = 0; self.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
[self.backgroundKeepAliveTimer invalidate];
if (!self.didFetchInBackground) {
[self backgroundConnectTimedOut];
}
[TSSocketManager resignActivity];
[[UIApplication sharedApplication] endBackgroundTask:identifier]; [[UIApplication sharedApplication] endBackgroundTask:identifier];
} }
@ -262,7 +301,6 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification]; [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
} }
#pragma mark UI Delegates #pragma mark UI Delegates
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

Loading…
Cancel
Save