How to use the firebase/app.messaging function in firebase

To help you get started, we’ve selected a few firebase examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prescottprue / generator-react-firebase / generators / app / templates / src / utils / firebaseMessaging.js View on Github external
function getMessagingToken() {
  return firebase
    .messaging()
    .getToken()
    .catch(err => {
      console.error('Unable to retrieve refreshed token ', err) // eslint-disable-line no-console
      return Promise.reject(err)
    })
}
github yunity / karrot-frontend / src / service-worker.js View on Github external
*
 *  For development it will be built ONCE when you run `yarn dev`, after that you have to run it manually.
 *
 *  If you want to actually use it you must have FCM_SENDER_ID environment variable set to a valid value.
 *
 *  See https://docs.karrot.world/mobile.html for a hint ... you'll need to dig around a bit more on
 *  https://console.firebase.google.com/ though to find the sender id.
 *
 */

import 'firebase/messaging'
import { initializeApp, messaging as initializeMessaging } from 'firebase/app'

initializeApp({ messagingSenderId: __ENV.FCM_SENDER_ID })

const messaging = initializeMessaging()

messaging.setBackgroundMessageHandler(payload => {
  // not actually used, but without it here firefox does not receive messages...
  console.log('received payload', payload)
})

// Ensure new workers to replace old ones...
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting

self.addEventListener('install', event => {
  event.waitUntil(self.skipWaiting())
})

self.addEventListener('activate', event => {
  event.waitUntil(self.clients.claim())
})
github GoogleChromeLabs / gulliver / public / js / messaging.js View on Github external
unsubscribe(topic) {
    console.log('Unsubscribing from: ' + topic);
    const messaging = firebase.messaging();
    return messaging.getToken()
      .then(token => {
        const url = UNSUBSCRIBE_ENDPOINT + '/' + topic;
        return this._postWithToken(url, token);
      }).catch(err => {
        if (this._checkBlockedNotification(err)) {
          err.blocked = true;
        }
        return Promise.reject(err);
      });
  }
github swaponline / swap.react / shared / helpers / firebase / index.js View on Github external
new Promise(async (resolve) => {
    const messaging = firebase.messaging()

    await messaging.requestPermission()
      .then(() => messaging.getToken())
      .then((token) => resolve(token))
      .catch((error) => {
        console.log(error)
        resolve(false)
      })
  })
github fuhcm / fptu-app / src / firebase.js View on Github external
export const askForPermissionToReceiveNotifications = async () => {
  try {
    const messaging = firebase.messaging();
    await messaging.requestPermission();
    const token = await messaging.getToken();

    await FPTUSDK.push.syncPush(token);

    return token;
  } catch (err) {
    //eslint-disable-next-line
    console.log(err);
    return null;
  }
};
github zeit / next.js / examples / with-firebase-cloud-messaging / utils / webPush.js View on Github external
init: async function() {
    firebase.initializeApp({
      messagingSenderId: 'your sender id',
    })

    try {
      if ((await this.tokenInlocalforage()) !== null) {
        return false
      }

      const messaging = firebase.messaging()
      await messaging.requestPermission()
      const token = await messaging.getToken()

      localforage.setItem('fcm_token', token)
      console.log('fcm_token', token)
    } catch (error) {
      console.error(error)
    }
  },
}
github GoogleChromeLabs / gulliver / public / js / messaging.js View on Github external
isNotificationBlocked() {
    const messaging = firebase.messaging();
    return messaging.getToken()
      .then(_ => {
        return false;
      })
      .catch(err => {
        if (err.code === ERROR_NOTIFICATIONS_BLOCKED) {
          return true;
        }
        return Promise.reject(err);
      });
  }
}
github lucasinocente / simple-messenger / src / firebase / Firebase.js View on Github external
import firebase from "firebase/app";
import 'firebase/database';
import "firebase/messaging";

const firebaseConfig = {
    apiKey: process.env.REACT_APP_API_KEY,
    authDomain: process.env.REACT_APP_AUTH_DOMAIN,
    databaseURL: process.env.REACT_APP_DATABASE_URL,
    projectId: process.env.REACT_APP_PROJECT_ID,
    messagingSenderId: process.env.REACT_APP_MESSEAGING_SENDER_ID,
    appId: process.env.REACT_APP_APP_ID
};

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();

messaging.usePublicVapidKey(process.env.REACT_APP_WEB_PUSH_CERTIFICATES);

export { firebase as default, messaging };
github fuhcm / fptu-app / src / firebase.js View on Github external
.then(registration => {
        firebase.messaging().useServiceWorker(registration);
      });
  }