Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Push: Add logs to track spontaneously disabling #535

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions MatrixKit/Models/Account/MXKAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ - (void)setAntivirusServerURL:(NSString *)antivirusServerURL
- (void)setPushGatewayURL:(NSString *)pushGatewayURL
{
_pushGatewayURL = pushGatewayURL.length ? pushGatewayURL : nil;

NSLog(@"[MXKAccount][Push] setPushGatewayURL: %@", _pushGatewayURL);

// Archive updated field
[[MXKAccountManager sharedManager] saveAccounts];
Expand Down Expand Up @@ -373,11 +375,16 @@ - (UIColor*)userTintColor

- (BOOL)pushNotificationServiceIsActive
{
return ([[MXKAccountManager sharedManager] isAPNSAvailable] && _enablePushNotifications && mxSession);
BOOL pushNotificationServiceIsActive = ([[MXKAccountManager sharedManager] isAPNSAvailable] && _enablePushNotifications && mxSession);
NSLog(@"[MXKAccount][Push] pushNotificationServiceIsActive: %@", @(pushNotificationServiceIsActive));

return pushNotificationServiceIsActive;
}

- (void)setEnablePushNotifications:(BOOL)enablePushNotifications
{
NSLog(@"[MXKAccount][Push] setEnablePushNotifications: %@", @(enablePushNotifications));

if (enablePushNotifications)
{
_enablePushNotifications = YES;
Expand Down Expand Up @@ -407,11 +414,16 @@ - (void)setEnablePushNotifications:(BOOL)enablePushNotifications

- (BOOL)isPushKitNotificationActive
{
return ([[MXKAccountManager sharedManager] isPushAvailable] && _enablePushKitNotifications && mxSession);
BOOL isPushKitNotificationActive = ([[MXKAccountManager sharedManager] isPushAvailable] && _enablePushKitNotifications && mxSession);
NSLog(@"[MXKAccount][Push] isPushKitNotificationActive: %@", @(isPushKitNotificationActive));

return isPushKitNotificationActive;
}

- (void)setEnablePushKitNotifications:(BOOL)enablePushKitNotifications
{
NSLog(@"[MXKAccount][Push] setEnablePushKitNotifications: %@", @(enablePushKitNotifications));

if (enablePushKitNotifications)
{
_enablePushKitNotifications = YES;
Expand All @@ -423,7 +435,7 @@ - (void)setEnablePushKitNotifications:(BOOL)enablePushKitNotifications
}
else if (_enablePushKitNotifications)
{
NSLog(@"[MXKAccount] Disable Push for %@ account", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] setEnablePushKitNotifications: Disable Push for %@ account", self.mxCredentials.userId);

// Delete the pusher, report the new value only on success.
[self enablePushKitPusher:NO
Expand All @@ -440,6 +452,8 @@ - (void)setEnablePushKitNotifications:(BOOL)enablePushKitNotifications

- (void)setEnableInAppNotifications:(BOOL)enableInAppNotifications
{
NSLog(@"[MXKAccount] setEnableInAppNotifications: %@", @(enableInAppNotifications));

_enableInAppNotifications = enableInAppNotifications;

// Archive updated field
Expand Down Expand Up @@ -1015,10 +1029,12 @@ - (void)reload:(BOOL)clearCache
// Refresh the APNS pusher state for this account on this device.
- (void)refreshAPNSPusher
{
NSLog(@"[MXKAccount][Push] refreshAPNSPusher");

// Check the conditions required to run the pusher
if (self.pushNotificationServiceIsActive)
{
NSLog(@"[MXKAccount] Refresh APNS pusher for %@ account", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] refreshAPNSPusher: Refresh APNS pusher for %@ account", self.mxCredentials.userId);

// Create/restore the pusher
[self enableAPNSPusher:YES
Expand All @@ -1034,14 +1050,16 @@ - (void)refreshAPNSPusher
else if (_enablePushNotifications && mxSession)
{
// Turn off pusher if user denied remote notification.
NSLog(@"[MXKAccount] Disable APNS pusher for %@ account (notifications are denied)", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] refreshAPNSPusher: Disable APNS pusher for %@ account (notifications are denied)", self.mxCredentials.userId);
[self enableAPNSPusher:NO success:nil failure:nil];
}
}

// Enable/Disable the APNS pusher for this account on this device on the Home Server.
- (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(void (^)(NSError *))failure
{
NSLog(@"[MXKAccount][Push] enableAPNSPusher: %@", @(enabled));

#ifdef DEBUG
NSString *appId = [[NSUserDefaults standardUserDefaults] objectForKey:@"pusherAppIdDev"];
#else
Expand All @@ -1052,7 +1070,7 @@ - (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(

[self enablePusher:enabled appId:appId token:[MXKAccountManager sharedManager].apnsDeviceToken pushData:pushData success:^{

NSLog(@"[MXKAccount] Succeeded to update APNS pusher for %@ (%d)", self.mxCredentials.userId, enabled);
NSLog(@"[MXKAccount][Push] enableAPNSPusher: Succeeded to update APNS pusher for %@ (%d)", self.mxCredentials.userId, enabled);

if (success)
{
Expand All @@ -1070,7 +1088,7 @@ - (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(
MXError *mxError = [[MXError alloc] initWithNSError:error];
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringUnknown])
{
NSLog(@"[MXKAccount] APNS was already disabled for %@!", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] enableAPNSPusher: APNS was already disabled for %@!", self.mxCredentials.userId);

// Ignore the error
if (success)
Expand All @@ -1083,11 +1101,11 @@ - (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(
return;
}

NSLog(@"[MXKAccount] Failed to disable APNS %@! (%@)", self.mxCredentials.userId, error);
NSLog(@"[MXKAccount][Push] enableAPNSPusher: Failed to disable APNS %@! (%@)", self.mxCredentials.userId, error);
}
else
{
NSLog(@"[MXKAccount] Failed to send APNS token for %@! (%@)", self.mxCredentials.userId, error);
NSLog(@"[MXKAccount][Push] enableAPNSPusher: Failed to send APNS token for %@! (%@)", self.mxCredentials.userId, error);
}

if (failure)
Expand All @@ -1102,10 +1120,12 @@ - (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(
// Refresh the PushKit pusher state for this account on this device.
- (void)refreshPushKitPusher
{
NSLog(@"[MXKAccount][Push] refreshPushKitPusher");

// Check the conditions required to run the pusher
if (self.isPushKitNotificationActive)
{
NSLog(@"[MXKAccount] Refresh PushKit pusher for %@ account", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] refreshPushKitPusher: Refresh PushKit pusher for %@ account", self.mxCredentials.userId);

// Create/restore the pusher
[self enablePushKitPusher:YES
Expand All @@ -1121,14 +1141,16 @@ - (void)refreshPushKitPusher
else if (_enablePushKitNotifications && mxSession)
{
// Turn off pusher if user denied remote notification.
NSLog(@"[MXKAccount] Disable PushKit pusher for %@ account (notifications are denied)", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] refreshPushKitPusher: Disable PushKit pusher for %@ account (notifications are denied)", self.mxCredentials.userId);
[self enablePushKitPusher:NO success:nil failure:nil];
}
}

// Enable/Disable the pusher based on PushKit for this account on this device on the Home Server.
- (void)enablePushKitPusher:(BOOL)enabled success:(void (^)(void))success failure:(void (^)(NSError *))failure
{
NSLog(@"[MXKAccount][Push] enablePushKitPusher: %@", @(enabled));

NSString *appId = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushKitAppIdProd"];

NSMutableDictionary *pushData = [NSMutableDictionary dictionaryWithDictionary:@{@"url": self.pushGatewayURL}];
Expand All @@ -1138,10 +1160,11 @@ - (void)enablePushKitPusher:(BOOL)enabled success:(void (^)(void))success failur
{
[pushData addEntriesFromDictionary:options];
}

[self enablePusher:enabled appId:appId token:[MXKAccountManager sharedManager].pushDeviceToken pushData:pushData success:^{

NSData *token = [MXKAccountManager sharedManager].pushDeviceToken;
[self enablePusher:enabled appId:appId token:token pushData:pushData success:^{

NSLog(@"[MXKAccount] Succeeded to update PushKit pusher for %@ (%d)", self.mxCredentials.userId, enabled);
NSLog(@"[MXKAccount][Push] enablePushKitPusher: Succeeded to update PushKit pusher for %@. Enabled: %@. Token: %@", self.mxCredentials.userId, @(enabled), [MXKTools logForPushToken:token]);

if (success)
{
Expand All @@ -1159,7 +1182,7 @@ - (void)enablePushKitPusher:(BOOL)enabled success:(void (^)(void))success failur
MXError *mxError = [[MXError alloc] initWithNSError:error];
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringUnknown])
{
NSLog(@"[MXKAccount] Push was already disabled for %@!", self.mxCredentials.userId);
NSLog(@"[MXKAccount][Push] enablePushKitPusher: Push was already disabled for %@!", self.mxCredentials.userId);

// Ignore the error
if (success)
Expand All @@ -1172,11 +1195,11 @@ - (void)enablePushKitPusher:(BOOL)enabled success:(void (^)(void))success failur
return;
}

NSLog(@"[MXKAccount] Failed to disable Push %@! (%@)", self.mxCredentials.userId, error);
NSLog(@"[MXKAccount][Push] enablePushKitPusher: Failed to disable Push %@! (%@)", self.mxCredentials.userId, error);
}
else
{
NSLog(@"[MXKAccount] Failed to send Push token for %@! (%@)", self.mxCredentials.userId, error);
NSLog(@"[MXKAccount][Push] enablePushKitPusher: Failed to send Push token for %@! (%@)", self.mxCredentials.userId, error);
}

if (failure)
Expand All @@ -1190,23 +1213,25 @@ - (void)enablePushKitPusher:(BOOL)enabled success:(void (^)(void))success failur

- (void)enablePusher:(BOOL)enabled appId:(NSString*)appId token:(NSData*)token pushData:(NSDictionary*)pushData success:(void (^)(void))success failure:(void (^)(NSError *))failure
{
NSLog(@"[MXKAccount][Push] enablePusher: %@", @(enabled));

// Refuse to try & turn push on if we're not logged in, it's nonsensical.
if (!mxCredentials)
{
NSLog(@"[MXKAccount] Not setting push token because we're not logged in");
NSLog(@"[MXKAccount][Push] enablePusher: Not setting push token because we're not logged in");
return;
}

// Check whether the Push Gateway URL has been configured.
if (!self.pushGatewayURL)
{
NSLog(@"[MXKAccount] Not setting pusher because the Push Gateway URL is undefined");
NSLog(@"[MXKAccount][Push] enablePusher: Not setting pusher because the Push Gateway URL is undefined");
return;
}

if (!appId)
{
NSLog(@"[MXKAccount] Not setting pusher because pusher app id is undefined");
NSLog(@"[MXKAccount][Push] enablePusher: Not setting pusher because pusher app id is undefined");
return;
}

Expand All @@ -1226,12 +1251,12 @@ - (void)enablePusher:(BOOL)enabled appId:(NSString*)appId token:(NSData*)token p
unsigned char c = [alphabet characterAtIndex:arc4random() % alphabet.length];
profileTag = [profileTag stringByAppendingFormat:@"%c", c];
}
NSLog(@"[MXKAccount] Generated fresh profile tag: %@", profileTag);
NSLog(@"[MXKAccount][Push] enablePusher: Generated fresh profile tag: %@", profileTag);
[[NSUserDefaults standardUserDefaults] setValue:profileTag forKey:@"pusherProfileTag"];
}
else
{
NSLog(@"[MXKAccount] Using existing profile tag: %@", profileTag);
NSLog(@"[MXKAccount][Push] enablePusher: Using existing profile tag: %@", profileTag);
}

NSObject *kind = enabled ? @"http" : [NSNull null];
Expand All @@ -1248,7 +1273,7 @@ - (void)enablePusher:(BOOL)enabled appId:(NSString*)appId token:(NSData*)token p
break;
}
}
NSLog(@"[MXKAccount] append flag: %d", append);
NSLog(@"[MXKAccount][Push] enablePusher: append flag: %d", append);

MXRestClient *restCli = self.mxRestClient;

Expand Down
Loading