JavaScript onesignal error: window.OneSignal already defined as 'object'!

I am getting this error when I am trying to subscribe someone to notifications on localhost with xampp (with a custom domain: servercode.com):

window.OneSignal already defined as ‘object’!
Please make sure to define as 'window.OneSignal = window.OneSignal || ;

And I am also getting another error saying:

Installing service worker failed SecurityError: Failed to register a ServiceWorker for scope (https://servercode.com/) with script (https://servercode.com/OneSignalSDKWorker.js?appId=48a8ea77-2f42-4f54-8f4f-f21f0e3d9b3b): An SSL certificate error occurred when fetching the script.

Here is my js code:

window.OneSignal = window.OneSignal || [];

  OneSignal.push(function() {

    OneSignal.init({

      appId: "key",

      notifyButton: {

        enable: true,

      },

      promptOptions: {

        slidedown: {

          enabled: true,

          autoPrompt: true,

          timeDelay: 20,

          pageViews: 3,

          actionMessage: "We'd like to show you notifications for the latest news and updates.",

        acceptButtonText: "ALLOW",

        cancelButtonText: "BLOCK",

        }

      }

    });

  });

And I did remember to put in:

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>

Can you help me?

The error says that you need an SSL certificate for your custom servercode.com domain.
I think that the people in our Server Config forum might have some help for you there.

Thanks!! After i fixed the certificate/ssl problem it fixed it! A long time ago, I was trying to get it working but I couldn’t. Now, I took the same tutorial, and it woks!

2 Likes