Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ******************** SDK set up and foreground message handling. ***************
- <script>
- // Initialize Firebase
- var config = {
- apiKey: "AIzaSyCpxieXvbvbUuQy9vFQFX1ZxvVKyID2chY",
- authDomain: "detroitechcallcenterapp.firebaseapp.com",
- databaseURL: "https://detroitechcallcenterapp.firebaseio.com",
- projectId: "detroitechcallcenterapp",
- storageBucket: "detroitechcallcenterapp.appspot.com",
- messagingSenderId: "94883233124"
- };
- firebase.initializeApp(config);
- const messaging = firebase.messaging();
- messaging.requestPermission().then(function () {
- console.log("Permission given");
- return messaging.getToken();
- }, function () {
- console.log("Permission dennied")
- }).then(function (token) {
- console.log("my token", token);
- });
- messaging.onMessage(function (payload) {
- console.log('Message received :: online. ', payload);
- // ...
- });
- </script>
- ******************** firebase-messaging-sw.js file in the root directory to handle background notification***************
- // Give the service worker access to Firebase Messaging.
- // Note that you can only use Firebase Messaging here, other Firebase libraries
- // are not available in the service worker.
- importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
- importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
- // Initialize the Firebase app in the service worker by passing in the
- // messagingSenderId.
- firebase.initializeApp({
- 'messagingSenderId': '94883233124'
- });
- // Retrieve an instance of Firebase Messaging so that it can handle background
- // messages.
- const messaging = firebase.messaging();
- // Handle incoming messages. Called when:
- // - a message is received while the app has focus
- // - the user clicks on an app notification created by a service worker
- // `messaging.setBackgroundMessageHandler` handler.
- messaging.setBackgroundMessageHandler(function(payload){
- const title = "Message from detroitech";
- const options = {
- body : payload.data.testMessage
- };
- return self.registration.showNotification(title,options);
- });
- messaging.setBackgroundMessageHandler(function (payload) {
- console.log('Handling background message ', payload);
- return self.registration.showNotification(payload.data.title, {
- body: payload.data.body,
- icon: payload.data.icon,
- tag: payload.data.tag,
- data: payload.data.link
- });
- });
- self.addEventListener('notificationclick', function(event) {
- event.notification.close();
- event.waitUntil(self.clients.openWindow(event.notification.data));
- });
Add Comment
Please, Sign In to add comment