Skip to content

Resetting the Push Receiver feature #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/electron-push-receiver.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,4 +9,4 @@ interface ElectronPushReceiver {
}

declare const electronPushReceiver: ElectronPushReceiver;
export = electronPushReceiver;
export = electronPushReceiver;
21 changes: 20 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +14,7 @@ const config = new Config();

module.exports = {
START_NOTIFICATION_SERVICE,
DESTROY_NOTIFICATION_SERVICE,
NOTIFICATION_SERVICE_STARTED,
NOTIFICATION_SERVICE_ERROR,
NOTIFICATION_RECEIVED,
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down