logo

registerToFCM

The registerToFCM method registers a device to Firebase Cloud Messaging. Optionally registers with already existing ACG ID and ACG security token.

  • The app ID is the package name of the app.
  • The ECE auth secret and ECE public key must be generated beforehand with the createFcmECDH and generateFcmAuthSecret functions. The auth secret and ECDH keys must be stored.
  • The Firebase API key, Firebase app ID and Firebase project ID can be found in the Firebase console.
  • The VAPID key can be found in the Firebase console.

Returns the ACG ID, ACG security token and FCM token, which must be stored.

The FCM token does not seem to expire.

import {
  createFcmECDH,
  generateFcmAuthSecret,
  registerToFCM
} from '@aracna/fcm';

const authSecret = generateFcmAuthSecret();
const ecdh = createFcmECDH();

registerToFCM({
  appID: 'YOUR_APP_ID',
  ece: {
    authSecret: authSecret,
    publicKey: ecdh.getPublicKey()
  },
  firebase: {
    apiKey: 'YOUR_FIREBASE_API_KEY',
    appID: 'YOUR_FIREBASE_APP_ID',
    projectID: 'YOUR_FIREBASE_PROJECT_ID'
  },
  vapidKey: 'YOUR_VAPID_KEY'
})
  .catch(() => {})
  .then((registration) => {
    // will log { acg: { id: bigint, securityToken: bigint }, token: string }
    console.log(registration);
  });