12

I think I'm missing something that is likely incredibly easy.

I'm registering a service worker to receive push notifications from my server via GCM. This works fine.

Unfortunately, I can't pass any data along with my push notification, so when the service worker receives the push, it doesn't know anything about it. So before I show the notification, I call back to my server to request additional information about the push, like the title, message and icon.

My problem is that when I call back to my server for additional information, before displaying the push, I don't have any information in the service worker about the user requesting additional information. In order to collect that information, I need to pass a user ID and token to my server.

So my question boils down to: How do grab the user id and token variables from my core.js code into the service worker so I can use them to gather additional information from my server?

In my web app, I'm registering the service worker like this:

core.js

var uid = "89123891231239"; // Dummy global id variable
var token = "eo4Dr8qhrFY"; // Dummy global token variable

function setupPushNotifications(){
    console.log("Setting up push notifications");

    if ('serviceWorker' in navigator) {

        navigator.serviceWorker.register('js/pushWorker.js').then(function(reg) {

            console.log(reg);

            reg.pushManager.subscribe({
                userVisibleOnly: true
            }).then(function(sub) {
                console.log('endpoint:', sub.endpoint);
            });

        }).catch(function(error) {
            console.log('Error: ', error);
        });
    }
}

Then, I have a service worker that looks like this:

pushWorker.js

'use strict';

self.addEventListener('install', function(event) {
    self.skipWaiting();
    console.log('Installed', event);
});

self.addEventListener('activate', function(event) {
    console.log('Activated', event);
});

self.addEventListener('push', function(event) {  

    var uid = "EXISTS_IN_CORE.JS";
    var token = "ALSO_EXISTS_IN_CORE.JS";

    console.log("Push recieved - we need to know the uid and token here, from core.js");

    event.waitUntil(  
        fetch("https://oapi.co/o?f=getExtraPushData&uid=" + uid + "&t=" + token).then(function(response) {  
          if (response.status !== 200) {  
            console.log('Looks like there was a problem. Status Code: ' + response.status);  
            throw new Error();  
          }

          return response.json().then(function(data) {  
            if (data.error || !data.notification) {  
              console.error('The API returned an error.', data.error);  
              throw new Error();  
            }  

            var title = data.notification.title;  
            var message = data.notification.message;  
            var icon = data.notification.icon;  

            return self.registration.showNotification(title, {  
              body: message,  
              icon: icon,  
            });  
          });  
        }).catch(function(err) {  
          console.error('Unable to retrieve data', err);

          var title = 'An error occurred';
          var message = 'We were unable to get the information for this push message';  
          var icon = "https://oapi.co/occurrences_secure/img/stepNotify_1.png";  
          var notificationTag = 'notification-error';  
          return self.registration.showNotification(title, {  
              body: message,  
              icon: icon,  
              tag: notificationTag  
            });  
        })  
    );  
});

I've read about the limitations of workers here: Accessing localStorage from a webWorker

And I am aware of the postMessage function, but I can't figure out how to make sense of it in practice.

Any help would be greatly appreciated.

Community
  • 1
  • 1
Ryan Martin
  • 1,613
  • 3
  • 24
  • 36
  • I don't see the token and user id in the core.js code you posted – neuhaus Mar 01 '16 at 14:39
  • I'll edit my post - I assumed it was clear that those variables do exist, despite not being listed. I don't need exact code showing how to pass those exact two variable, I'm looking for help on how the passing of data works, in general, when I receive a push. – Ryan Martin Mar 01 '16 at 14:42
  • You should use postMessage to send message to worker. – jcubic Mar 01 '16 at 14:46
  • well you have to pass them somehow if they are not in the global scope, that's what this question is about, right? – neuhaus Mar 01 '16 at 14:46
  • Please read my question. I literally wrote "And I am aware of the postMessage() function, but I can't figure out how to make sense of it in practice." – Ryan Martin Mar 01 '16 at 14:47
  • neuhaus, no, this question has nothing to do with the global scope of core.js. It has to do with requesting any data from core.js from within pushWorker.js. – Ryan Martin Mar 01 '16 at 14:50
  • what is GCM in your question stand for? – Ricky-U Apr 20 '21 at 02:14

1 Answers1

25

You can easily solve your problem like this:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('pushWorker.js').then(function() {
        return navigator.serviceWorker.ready;
    }).then(function(reg) {
        console.log('Service Worker is ready', reg);
        reg.pushManager.subscribe({userVisibleOnly: true}).then(function(sub) {

            console.log('endpoint:', sub.endpoint);
            reg.active.postMessage(JSON.stringify({uid: uid, token: token}));
            console.log("Posted message");

        });
    }).catch(function(error) {
        console.log('Error : ', error);
    });
}

The key here is...

reg.active.postMessage(JSON.stringify({uid: uid, token: token}));

...since at that point in the code, you'll have access to postMessage();

In your worker code, make sure you have something like:

self.addEventListener('message', function(event){
    var data = JSON.parse(event.data);

    console.log("SW Received Message:");
    console.log(data);

    self.userID = data.uid;
    self.userToken = data.token;

});

This will give you global access to your userID and userToken information when you eventually need to call your server and request additional push information.

JacobTheDev
  • 17,318
  • 25
  • 95
  • 158
  • Excellent! Thank you very much - tested and works perfectly. – Ryan Martin Mar 01 '16 at 17:25
  • 2
    Not sure about the worker life cycle: if the browser is restarted (but the webpage is not accessed) will the web worker restart too? If so, will the userId and userToken variables be lost? – Montaner Jun 22 '16 at 15:47
  • 5
    I received the error when using this: `Registration failed - missing applicationServerKey, and manifest empty or missing`. And finally managed to do the same by using postMessage directly: https://stackoverflow.com/a/28513836/454985 – jmu Jun 22 '17 at 19:36
  • The `applicationServerKey` error means you need to pass the VAPID public key. https://github.com/mozilla/serviceworker-cookbook/issues/307#issuecomment-394148836 – Nickofthyme May 17 '20 at 14:12