From a0104f5a1ede769ad42b2a7d8bbbecca3f4bd616 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 30 Oct 2020 13:54:45 +0100 Subject: [PATCH] MOBILE-3507 push: Call get_user_devices after register in Moodle --- .../messageoutput/airnotifier/airnotifier.module.ts | 9 ++++++++- .../messageoutput/airnotifier/providers/airnotifier.ts | 8 +++++++- .../pushnotifications/providers/pushnotifications.ts | 3 +++ src/providers/events.ts | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/addon/messageoutput/airnotifier/airnotifier.module.ts b/src/addon/messageoutput/airnotifier/airnotifier.module.ts index b8a3b3ca6..78a7fcff0 100644 --- a/src/addon/messageoutput/airnotifier/airnotifier.module.ts +++ b/src/addon/messageoutput/airnotifier/airnotifier.module.ts @@ -13,6 +13,7 @@ // limitations under the License. import { NgModule } from '@angular/core'; +import { CoreEventsProvider } from '@providers/events'; import { AddonMessageOutputDelegate } from '@addon/messageoutput/providers/delegate'; import { AddonMessageOutputAirnotifierProvider } from './providers/airnotifier'; import { AddonMessageOutputAirnotifierHandler } from './providers/handler'; @@ -28,7 +29,13 @@ import { AddonMessageOutputAirnotifierHandler } from './providers/handler'; ] }) export class AddonMessageOutputAirnotifierModule { - constructor(messageOutputDelegate: AddonMessageOutputDelegate, airnotifierHandler: AddonMessageOutputAirnotifierHandler) { + constructor(messageOutputDelegate: AddonMessageOutputDelegate, airnotifierHandler: AddonMessageOutputAirnotifierHandler, + eventsProvider: CoreEventsProvider, airnotifierProvider: AddonMessageOutputAirnotifierProvider) { messageOutputDelegate.registerHandler(airnotifierHandler); + + eventsProvider.on(CoreEventsProvider.DEVICE_REGISTERED_IN_MOODLE, async (data) => { + // Get user devices to make Moodle send the devices data to Airnotifier. + airnotifierProvider.getUserDevices(true, data.siteId); + }); } } diff --git a/src/addon/messageoutput/airnotifier/providers/airnotifier.ts b/src/addon/messageoutput/airnotifier/providers/airnotifier.ts index 01d429774..f29c4ba48 100644 --- a/src/addon/messageoutput/airnotifier/providers/airnotifier.ts +++ b/src/addon/messageoutput/airnotifier/providers/airnotifier.ts @@ -74,10 +74,11 @@ export class AddonMessageOutputAirnotifierProvider { /** * Get user devices. * + * @param ignoreCache Whether to ignore cache. * @param siteId Site ID. If not defined, use current site. * @return Promise resolved with the devices. */ - getUserDevices(siteId?: string): Promise { + getUserDevices(ignoreCache?: boolean, siteId?: string): Promise { this.logger.debug('Get user devices'); return this.sitesProvider.getSite(siteId).then((site) => { @@ -89,6 +90,11 @@ export class AddonMessageOutputAirnotifierProvider { updateFrequency: CoreSite.FREQUENCY_RARELY }; + if (ignoreCache) { + preSets['getFromCache'] = false; + preSets['emergencyCache'] = false; + } + return site.read('message_airnotifier_get_user_devices', data, preSets) .then((data: AddonMessageOutputAirnotifierGetUserDevicesResult) => { return data.devices; diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts index 7c8449406..f5fa633d4 100644 --- a/src/core/pushnotifications/providers/pushnotifications.ts +++ b/src/core/pushnotifications/providers/pushnotifications.ts @@ -19,6 +19,7 @@ import { Push, PushObject, PushOptions } from '@ionic-native/push'; import { Device } from '@ionic-native/device'; import { TranslateService } from '@ngx-translate/core'; import { CoreApp, CoreAppProvider, CoreAppSchema } from '@providers/app'; +import { CoreEvents, CoreEventsProvider } from '@providers/events'; import { CoreInitDelegate } from '@providers/init'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider, CoreSiteSchema } from '@providers/sites'; @@ -789,6 +790,8 @@ export class CorePushNotificationsProvider { // Now register the device. await site.write('core_user_add_user_device', this.utils.clone(data)); + CoreEvents.instance.trigger(CoreEventsProvider.DEVICE_REGISTERED_IN_MOODLE, {}, site.getId()); + // Insert the device in the local DB. try { await site.getDb().insertRecord(CorePushNotificationsProvider.REGISTERED_DEVICES_TABLE, data); diff --git a/src/providers/events.ts b/src/providers/events.ts index 3974e7281..1dfcf409d 100644 --- a/src/providers/events.ts +++ b/src/providers/events.ts @@ -68,6 +68,7 @@ export class CoreEventsProvider { static SITE_STORAGE_DELETED = 'site_storage_deleted'; static FORM_ACTION = 'form_action'; static ACTIVITY_DATA_SENT = 'activity_data_sent'; + static DEVICE_REGISTERED_IN_MOODLE = 'device_registered_in_moodle'; protected logger; protected observables: { [s: string]: Subject } = {};