diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts index de508f430..c03e2be92 100644 --- a/src/core/pushnotifications/providers/pushnotifications.ts +++ b/src/core/pushnotifications/providers/pushnotifications.ts @@ -292,6 +292,29 @@ export class CorePushNotificationsProvider { return this.getAddonBadge(siteId); } + /** + * Log a firebase event. + * + * @param {string} name Name of the event. + * @param {any} data Data of the event. + * @param {boolean} [filter] Whether to filter the data. This is useful when logging a full notification. + * @return {Promise} Promise resolved when done. This promise is never rejected. + */ + logEvent(name: string, data: any, filter?: boolean): Promise { + const win = window; // This feature is only present in our fork of the plugin. + + if (win.PushNotification && win.PushNotification.logEvent) { + return new Promise((resolve, reject): void => { + win.PushNotification.logEvent(resolve, (error) => { + this.logger.error('Error logging firebase event', name, error); + resolve(); + }, name, data, !!filter); + }); + } + + return Promise.resolve(); + } + /** * Function called when a push notification is clicked. Redirect the user to the right state. * diff --git a/src/core/pushnotifications/pushnotifications.module.ts b/src/core/pushnotifications/pushnotifications.module.ts index d8d562108..d804e4270 100644 --- a/src/core/pushnotifications/pushnotifications.module.ts +++ b/src/core/pushnotifications/pushnotifications.module.ts @@ -80,8 +80,12 @@ export class CorePushNotificationsModule { }); // Listen for local notification clicks (generated by the app). - localNotificationsProvider.registerClick(CorePushNotificationsProvider.COMPONENT, - pushNotificationsProvider.notificationClicked.bind(pushNotificationsProvider)); + localNotificationsProvider.registerClick(CorePushNotificationsProvider.COMPONENT, (notification: any) => { + // Log notification open event. + pushNotificationsProvider.logEvent('moodle_notification_open', notification, true); + + pushNotificationsProvider.notificationClicked(notification); + }); // Allow migrating the table from the old app to the new schema. updateManager.registerAppTableMigration({