From 230a16c7775bf318f5664dca6d2e5269a55e15aa Mon Sep 17 00:00:00 2001 From: Victor Kozodaev Date: Wed, 13 May 2020 19:47:46 +0300 Subject: [PATCH 1/2] add methods to destroy push service and clear electron cache --- src/constants/index.js | 2 ++ src/electron-push-receiver.d.ts | 3 ++- src/index.js | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/constants/index.js b/src/constants/index.js index 899ae04..8bb5c4e 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -1,6 +1,8 @@ module.exports = { // Event to be sent from renderer process to trigger service start START_NOTIFICATION_SERVICE: 'PUSH_RECEIVER:::START_NOTIFICATION_SERVICE', + // Event to be sent from renderer process to trigger service destroy + DESTROY_NOTIFICATION_SERVICE: 'PUSH_RECEIVER:::DESTROY_NOTIFICATION_SERVICE', // Event sent to the renderer process once the service is up NOTIFICATION_SERVICE_STARTED: 'PUSH_RECEIVER:::NOTIFICATION_SERVICE_STARTED', // Event sent to the renderer process if an error has occured during the starting process diff --git a/src/electron-push-receiver.d.ts b/src/electron-push-receiver.d.ts index 88ffeec..0591f5e 100644 --- a/src/electron-push-receiver.d.ts +++ b/src/electron-push-receiver.d.ts @@ -1,5 +1,6 @@ interface ElectronPushReceiver { START_NOTIFICATION_SERVICE: string; + DESTROY_NOTIFICATION_SERVICE: string; NOTIFICATION_SERVICE_STARTED: string; NOTIFICATION_SERVICE_ERROR: string; NOTIFICATION_RECEIVED: string; @@ -8,4 +9,4 @@ interface ElectronPushReceiver { } declare const electronPushReceiver: ElectronPushReceiver; -export = electronPushReceiver; \ No newline at end of file +export = electronPushReceiver; diff --git a/src/index.js b/src/index.js index b30b64b..71203ff 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const { ipcMain } = require('electron'); const Config = require('electron-config'); const { START_NOTIFICATION_SERVICE, + DESTROY_NOTIFICATION_SERVICE, NOTIFICATION_SERVICE_STARTED, NOTIFICATION_SERVICE_ERROR, NOTIFICATION_RECEIVED, @@ -13,6 +14,7 @@ const config = new Config(); module.exports = { START_NOTIFICATION_SERVICE, + DESTROY_NOTIFICATION_SERVICE, NOTIFICATION_SERVICE_STARTED, NOTIFICATION_SERVICE_ERROR, NOTIFICATION_RECEIVED, @@ -23,6 +25,8 @@ module.exports = { // To be sure that start is called only once let started = false; +// used as a ref to client instance +let client; // To be call from the main process function setup(webContents) { // Will be called by the renderer process @@ -50,7 +54,10 @@ function setup(webContents) { webContents.send(TOKEN_UPDATED, credentials.fcm.token); } // Listen for GCM/FCM notifications - await listen(Object.assign({}, credentials, { persistentIds }), onNotification(webContents)); + client = await listen( + Object.assign({}, credentials, { persistentIds }), + onNotification(webContents), + ); // Notify the renderer process that we are listening for notifications webContents.send(NOTIFICATION_SERVICE_STARTED, credentials.fcm.token); } catch (e) { @@ -59,6 +66,18 @@ function setup(webContents) { webContents.send(NOTIFICATION_SERVICE_ERROR, e.message); } }); + + ipcMain.on(DESTROY_NOTIFICATION_SERVICE, () => { + // destroy push notifications service + if (client !== undefined) { + client.destroy(); + } + // clear cache + config.set('credentials', null); + config.set('senderId', null); + config.set('persistentIds', null); + started = false; + }); } // Will be called on new notification From d73a264c6ab6cb2d488b17965e433e6d1b15a2da Mon Sep 17 00:00:00 2001 From: Victor Kozodaev Date: Wed, 13 May 2020 20:07:58 +0300 Subject: [PATCH 2/2] add guide how to use destroy functionality --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index b9e00bc..54d756a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,22 @@ ipcRenderer.on(ON_NOTIFICATION_RECEIVED, (_, notification) => // display notific ipcRenderer.send(START_NOTIFICATION_SERVICE, senderId); ``` +## Resetting the Push Receiver + +There are cases where you may need to reset the push receiver to a state where it retrieves a new notification token from FCM. For instance, if your app is designed to support a sign-in screen and you only want push notifications for the person who signs in, you will need to have the push receiver delete the notification token when a different person signs in, otherwise the new sign-in may receive notifications that are only private to the person who signed in previously. + +- In renderer process : + +```javascript +import { ipcRenderer } from 'electron'; +import { + DESTROY_NOTIFICATION_SERVICE +} from 'electron-push-receiver/src/constants'; + +// You can invoke it on user logout +ipcRenderer.send(DESTROY_NOTIFICATION_SERVICE); +``` + ## Example Thanks to [CydeSwype](https://github.com/CydeSwype), you can find an example project [here](https://github.com/CydeSwype/electron-fcm-demo).