Skip to content

Commit

Permalink
Merge pull request #6815 from vector-im/gil/6814-Check_enabled_field_…
Browse files Browse the repository at this point in the history
…in_notification_settings_push_toggles

Check enabled field in notification settings push toggles
  • Loading branch information
gileluard authored Oct 6, 2022
2 parents 1783fac + 13cbfaf commit 4eb7a87
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Riot/Modules/MatrixKit/Models/Account/MXKAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ typedef BOOL (^MXKAccountOnCertificateChange)(MXKAccount *mxAccount, NSData *cer
*/
- (void)load3PIDs:(void (^)(void))success failure:(void (^)(NSError *error))failure;

/**
Loads the pusher instance linked to this account.
This method must be called to refresh self.pushNotificationServiceIsActive
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
*/
- (void)loadCurrentPusher:(nullable void (^)(void))success failure:(nullable void (^)(NSError *error))failure;

/**
Load the current device information for this account.
This method must be called to refresh self.device.
Expand Down
153 changes: 145 additions & 8 deletions Riot/Modules/MatrixKit/Models/Account/MXKAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ @interface MXKAccount ()

// Observe NSCurrentLocaleDidChangeNotification to refresh MXRoomSummaries on time formatting change.
id NSCurrentLocaleDidChangeNotificationObserver;

MXPusher *currentPusher;
}

/// Will be true if the session is not in a pauseable state or we requested for the session to pause but not finished yet. Will be reverted to false again after `resume` called.
Expand Down Expand Up @@ -149,6 +151,7 @@ - (nonnull instancetype)initWithCredentials:(MXCredentials*)credentials

// Refresh device information
[self loadDeviceInformation:nil failure:nil];
[self loadCurrentPusher:nil failure:nil];

[self registerAccountDataDidChangeIdentityServerNotification];
[self registerIdentityServiceDidChangeAccessTokenNotification];
Expand Down Expand Up @@ -184,6 +187,7 @@ - (id)initWithCoder:(NSCoder *)coder

// Refresh device information
[self loadDeviceInformation:nil failure:nil];
[self loadCurrentPusher:nil failure:nil];
}

return self;
Expand Down Expand Up @@ -303,6 +307,12 @@ - (UIColor*)userTintColor

- (BOOL)pushNotificationServiceIsActive
{
if (currentPusher && currentPusher.enabled)
{
MXLogDebug(@"[MXKAccount][Push] pushNotificationServiceIsActive: currentPusher.enabled %@", currentPusher.enabled);
return currentPusher.enabled.boolValue;
}

BOOL pushNotificationServiceIsActive = ([[MXKAccountManager sharedManager] isAPNSAvailable] && self.hasPusherForPushNotifications && mxSession);
MXLogDebug(@"[MXKAccount][Push] pushNotificationServiceIsActive: %@", @(pushNotificationServiceIsActive));

Expand All @@ -317,7 +327,44 @@ - (void)enablePushNotifications:(BOOL)enable

if (enable)
{
if ([[MXKAccountManager sharedManager] isAPNSAvailable])
if (currentPusher && currentPusher.enabled && !currentPusher.enabled.boolValue)
{
[self.mxSession.matrixRestClient setPusherWithPushkey:currentPusher.pushkey
kind:currentPusher.kind
appId:currentPusher.appId
appDisplayName:currentPusher.appDisplayName
deviceDisplayName:currentPusher.deviceDisplayName
profileTag:currentPusher.profileTag
lang:currentPusher.lang
data:currentPusher.data.JSONDictionary
append:NO
enabled:enable
success:^{

MXLogDebug(@"[MXKAccount][Push] enablePushNotifications: remotely enabled Push: Success");
[self loadCurrentPusher:^{
if (success)
{
success();
}
} failure:^(NSError *error) {

MXLogWarning(@"[MXKAccount][Push] enablePushNotifications: load current pusher failed with error: %@", error);
if (failure)
{
failure(error);
}
}];
} failure:^(NSError *error) {

MXLogWarning(@"[MXKAccount][Push] enablePushNotifications: remotely enable push failed with error: %@", error);
if (failure)
{
failure(error);
}
}];
}
else if ([[MXKAccountManager sharedManager] isAPNSAvailable])
{
MXLogDebug(@"[MXKAccount][Push] enablePushNotifications: Enable Push for %@ account", self.mxCredentials.userId);

Expand Down Expand Up @@ -354,7 +401,7 @@ - (void)enablePushNotifications:(BOOL)enable
}
}
}
else if (self.hasPusherForPushNotifications)
else if (self.hasPusherForPushNotifications || currentPusher)
{
MXLogDebug(@"[MXKAccount][Push] enablePushNotifications: Disable APNS for %@ account", self.mxCredentials.userId);

Expand Down Expand Up @@ -626,6 +673,65 @@ - (void)load3PIDs:(void (^)(void))success failure:(void (^)(NSError *))failure
}];
}

- (void)loadCurrentPusher:(void (^)(void))success failure:(void (^)(NSError *error))failure
{
if (!self.mxSession.myDeviceId)
{
MXLogWarning(@"[MXKAccount] loadPusher: device ID not found");
if (failure)
{
failure([NSError errorWithDomain:kMXKAccountErrorDomain code:0 userInfo:nil]);
}
return;
}

[self.mxSession supportedMatrixVersions:^(MXMatrixVersions *matrixVersions) {
if (!matrixVersions.supportsRemotelyTogglingPushNotifications)
{
MXLogDebug(@"[MXKAccount] loadPusher: remotely toggling push notifications not supported");

if (success)
{
success();
}

return;
}

[self.mxSession.matrixRestClient pushers:^(NSArray<MXPusher *> *pushers) {
MXPusher *ownPusher;
for (MXPusher *pusher in pushers)
{
if ([pusher.deviceId isEqualToString:self.mxSession.myDeviceId])
{
ownPusher = pusher;
}
}

self->currentPusher = ownPusher;

if (success)
{
success();
}
} failure:^(NSError *error) {
MXLogWarning(@"[MXKAccount] loadPusher: get pushers failed due to error %@", error);

if (failure)
{
failure(error);
}
}];
} failure:^(NSError *error) {
MXLogWarning(@"[MXKAccount] loadPusher: supportedMatrixVersions failed due to error %@", error);

if (failure)
{
failure(error);
}
}];
}

- (void)loadDeviceInformation:(void (^)(void))success failure:(void (^)(NSError *error))failure
{
if (self.mxCredentials.deviceId)
Expand Down Expand Up @@ -773,7 +879,9 @@ -(void)openSessionWithStore:(id<MXStore>)store
[MXKContactManager.sharedManager validateSyncLocalContactsStateForSession:self.mxSession];

// Refresh pusher state
[self refreshAPNSPusher];
[self loadCurrentPusher:^{
[self refreshAPNSPusher];
} failure:nil];
[self refreshPushKitPusher];

// Launch server sync
Expand Down Expand Up @@ -1106,6 +1214,12 @@ - (void)reload:(BOOL)clearCache
- (void)refreshAPNSPusher
{
MXLogDebug(@"[MXKAccount][Push] refreshAPNSPusher");

if (currentPusher)
{
MXLogDebug(@"[MXKAccount][Push] refreshAPNSPusher aborted as a pusher has been found");
return;
}

// Check the conditions required to run the pusher
if (self.pushNotificationServiceIsActive)
Expand Down Expand Up @@ -1165,12 +1279,35 @@ - (void)enableAPNSPusher:(BOOL)enabled success:(void (^)(void))success failure:(
self->_hasPusherForPushNotifications = enabled;
[[MXKAccountManager sharedManager] saveAccounts];

if (success)
if (enabled)
{
success();
[self loadCurrentPusher:^{
if (success)
{
success();
}

[[NSNotificationCenter defaultCenter] postNotificationName:kMXKAccountAPNSActivityDidChangeNotification object:self.mxCredentials.userId];
} failure:^(NSError *error) {
if (success)
{
success();
}

[[NSNotificationCenter defaultCenter] postNotificationName:kMXKAccountAPNSActivityDidChangeNotification object:self.mxCredentials.userId];
}];
}
else
{
self->currentPusher = nil;

if (success)
{
success();
}

[[NSNotificationCenter defaultCenter] postNotificationName:kMXKAccountAPNSActivityDidChangeNotification object:self.mxCredentials.userId];
}

[[NSNotificationCenter defaultCenter] postNotificationName:kMXKAccountAPNSActivityDidChangeNotification object:self.mxCredentials.userId];

} failure:^(NSError *error) {

Expand Down Expand Up @@ -1415,7 +1552,7 @@ - (void)enablePusher:(BOOL)enabled appId:(NSString*)appId token:(NSData*)token p

MXRestClient *restCli = self.mxRestClient;

[restCli setPusherWithPushkey:b64Token kind:kind appId:appId appDisplayName:appDisplayName deviceDisplayName:[[UIDevice currentDevice] name] profileTag:profileTag lang:deviceLang data:pushData append:append success:success failure:failure];
[restCli setPusherWithPushkey:b64Token kind:kind appId:appId appDisplayName:appDisplayName deviceDisplayName:[[UIDevice currentDevice] name] profileTag:profileTag lang:deviceLang data:pushData append:append enabled:enabled success:success failure:failure];
}

#pragma mark - InApp notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {

switch response {
case .success:
if let account = MXKAccountManager.shared().activeAccounts.first, account.device?.deviceId == pusher.deviceId {
account.loadCurrentPusher(nil)
}

self.checkPusher()
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] togglePusher failed due to error: \(error)")
Expand Down
1 change: 1 addition & 0 deletions changelog.d/6814.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check enabled field in notification settings push toggles

0 comments on commit 4eb7a87

Please sign in to comment.