1. Miscellaneous
  2. Triggering and Responding to Push Notifications

Miscellaneous

Triggering and Responding to Push Notifications

Start a push notifications service

To start the push notifications service all you need are the following details, which you can retrieve from the Project settings page in the Firebase Console:

  • projectID - The project ID in "Your project" of "General" tab in Firebase project settings
  • apiKey - The Web API key in "Your project" of "General" tab in Firebase project settings
  • appID - The app ID in in "Your apps" of "General" tab in Firebase project settings
  • vapidKey - The public key pair in "Web configuration" of "Cloud Messaging" tab in Firebase project settings. You will need to click "Generate key pair" if the key pair is not already generated.

With these details you can start the push notifications service by calling the start method and passing in the options outlined above.

NOTE

If you have ever created an application before ToDesktop Builder v0.23.1 then you should check the supportsNewFCMService() method returns true before starting the push notifications service. This will ensure that you don't get a runtime error with older applications.

        import { pushNotifications } from '@todesktop/client-core';

// This API requires `@todesktop/[email protected]` or later and ToDesktop Builder `v0.23.1` or later
if (pushNotifications.supportsNewFCMService()) {
  pushNotifications.start({
    projectID: 'myapp-prod',
    apiKey: 'AIzbSwB3bfv74OgIezU160U...',
    appID: '1:434372690918:web:57b4d513...',
    vapidKey: 'BMm6qW7_ae3FB_ow3fSSFrI...'
  });
}

      

Support legacy applications

You can also choose to support the legacy push notifications service which only required a senderID.

        import { pushNotifications } from '@todesktop/client-core';

if (pushNotifications.supportsNewFCMService()) {
  pushNotifications.start({
    projectID: 'myapp-prod',
    apiKey: 'AIzbSwB3bfv74OgIezU160U...',
    appID: '1:434372690918:web:57b4d513...',
    vapidKey: 'BMm6qW7_ae3FB_ow3fSSFrI...'
  });
} else {
  // Fallback to legacy push notifications service used in ToDesktop Builder `v0.22.3` or earlier
  pushNotifications.start('12345678901');
}

      

Respond to push notification "start" events

Triggered when the push notifications service is started successfully and an FCM registration token has been received.

        import { pushNotifications } from '@todesktop/client-core';

pushNotifications.on('start', (e, token) => {
  console.log(`Your FCM token is: ${token}`);
});

      

Respond to push notification "receive" events

Triggered when a push notifications service is received.

        import { pushNotifications } from '@todesktop/client-core';

pushNotifications.on('receive', (e, notification) => {
  console.log(notification);
  /**
   * {
   * 	"from": "14017693762",
   * 	"priority": "normal",
   * 	"notification": {
   * 		"title": "Hello World",
   * 		"body": "FooBar"
   * 	},
   * 	"collapse_key": "do_not_collapse"
   * }
   **/
});

      

Respond to push notification "error" events

Triggered when the push notifications service is started successfully and an FCM registration token has been received.

        import { pushNotifications } from '@todesktop/client-core';

pushNotifications.on('error', (e, error) => {
  // Notify user of error
});

      

Respond to push notification "tokenUpdate" events

Triggered when the FCM registration token has been updated.

        import { pushNotifications } from '@todesktop/client-core';

pushNotifications.on('tokenUpdate', (e, token) => {
  console.log(`Your FCM token has been updated to: ${token}`);
});