Forum on Firebase configuration and utilities

How to configure Firebase for push notification in WordPress

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #123
    Murali
    Keymaster

    Following is javascript invoked by WordPress plugin to configure Firebase to register service worker for push notification,
    Following variables are from enqueuing of script from plugin php file,

    var firebaseConfig = {
        apiKey: pnfpb_ajax_object_push.apiKey,
        authDomain: pnfpb_ajax_object_push.authDomain,
        databaseURL: pnfpb_ajax_object_push.databaseURL,
        projectId: pnfpb_ajax_object_push.projectId,
        storageBucket: pnfpb_ajax_object_push.storageBucket,
        messagingSenderId: pnfpb_ajax_object_push.messagingSenderId,
        appId: pnfpb_ajax_object_push.appId
    };
    
    // Initialize Firebase
    if (!firebase.apps.length) {
       firebase.initializeApp(firebaseConfig);
    } else {
       firebase.app(); // if already initialized, use that one
    }
    
    if ('serviceWorker' in navigator) {
    
    	
    navigator.serviceWorker.register(homeurl+'/pnfpb_sw.js',{scope:homeurl+'/'}).then(function(registration) {
        	registration.update();	
         	// If updatefound is fired, it means that there's
          	// a new service worker being installed.
          	//var installingWorker = registration.installing;
          registration.onupdatefound = () => {
            var installingWorker = registration.installing;
            installingWorker.onstatechange = () => {
              if (installingWorker.state === 'installed') {
                if (navigator.serviceWorker.controller) {
                  // At this point, the old content will have been purged and
                  // the fresh content will have been added to the cache.
                  // It's the perfect time to display a "New content is
                  // available; please refresh." message in your web app.
                  console.log('New content is available; please refresh.');
                 
                } else {
                  // At this point, everything has been precached.
                  // It's the perfect time to display a
                  // "Content is cached for offline use." message.
                  console.log('Content is cached for offline use1');
                }
         
              }
            };
          }; 
    //remaining code......
    });

    How to get FireBase configuration for WordPress,
    Sign in to Firebase, then open your project, click settings icon & select Project settings
    TO GET FIREBASE SERVER KEY
    project settings > cloud messaging tab > get server key or add server key button to get server key
    TO GET FIREBASE CONFIG FIELDS
    3.If you do not have web app, Create a new web app. After creating a new app, it will show firebase config fields
    Project settings > General under your apps section > click on config button to view configuration fields

    TO GET FIREBASE PUBLIC KEY
    Open the Cloud Messaging tab of the Firebase console Settings pane and scroll to the Web configuration section.
    In the Web Push certificates tab, click Generate Key Pair. The console displays a notice that the key pair was generated, and displays the public key string and date added.
    (If you already Generated key pair then no need to generate it again)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.