I am trying to integrate OneSignal with my Angular 5 app. I have placed manifest.json, OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js inside src folder and made entry under assets in .angular-cli.json so that these files become accessible, but I am still facing this issue of service worker installation failure. Error is GET http://localhost:4200/OneSignalSDKWorker.js?appId=xxx-xxx--xxx 404 In app.component.ts I have placed the initialization code but still I am getting error: [Service Worker Installation] Installing service worker failed TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

ngOnInit(){

    var OneSignal = window['OneSignal'] || [];
    console.log("Init OneSignal");
    OneSignal.push(["init", {
        appId: "xxx-xxx-xxx-xxx-xxx",
        autoRegister: false,
        allowLocalhostAsSecureOrigin: true,
        notifyButton: {
            enable: true
        }
    }]);
    console.log('OneSignal Initialized');
    OneSignal.push(function () {
        console.log('Register For Push');
        OneSignal.push(["registerForPushNotifications"]);
    });
    OneSignal.push(function () {
        OneSignal.on('subscriptionChange', function (isSubscribed) {
        console.log("The user's subscription state is now:", isSubscribed);
            OneSignal.getUserId().then(function (userId) {
                console.log("User ID is", userId);
            });
        });
    });
}

1 Answers

0
Community On

I don't think that it will work on localhost, you will need to publish it to a separate hosting (you can use Firebase, it's free).

The simplest solution would be to just put the script provided by One Signal into your index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
      name="viewport"
    />

    ...
    ...

    <link rel="manifest" href="/manifest.json" />
    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
    <script>
      var OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
        OneSignal.init({
          appId: 'xxx-xxxx-xxxxx',
        });
      });
    </script>

    ...

Add the following into angular.json


    ...
    "architect": {
        "build": {
          "builder": "ngx-build-plus:build",
          "options": {
            "outputPath": "dist/browser",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/favicon.png",
              "src/manifest.json", <-- this 
              "src/OneSignalSDKUpdaterWorker.js", <-- this 
              "src/OneSignalSDKWorker.js", <-- and finally this one
              "src/assets"
            ],
            "styles": [ 
              "src/styles.scss"
            ],
            "stylePreprocessorOptions": {
              "includePaths": ["src/assets/sass"]
            },
            "scripts": []
          },
          ...

And place the manifest and the 2 *.js file into /src/*

That's it.

I got it to work on mine using this method. Hope that helps