OneSignal Web Push stopped working?


#1

Hi,

So this was working fine and after and Ionic 3 version update, I’m getting the below error,

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'OneSignal' of undefined

I have the below in my index.html,

 <!-- un-comment this code to enable web push -->
  <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async='async'></script>
  <script>
    var OneSignal = window.OneSignal || [];
  </script>
  <!-- -->

and then I init it as below to try to obtain my playerId.

initWebPush()
{
  var that = this;

  this.OneSignal = window["OneSignal"];
  this.oneSignalId = "";

  
  this.OneSignal.push(["init", {
    appId: "xxxxxxx-xxxx-xxxxxx-xxxxxxx",
    autoRegister: true,
    httpPermissionRequest: {
      enable: true
    },
    welcomeNotification: {
      disable: true
    },
    notifyButton: {
      enable: false
    },
    promptOptions: {
      siteName: 'Push Notifications',                                   
      actionMessage: "Please Allow Push Notifications.",               
      exampleNotificationTitle: 'Holiday Approved',
      exampleNotificationMessage: 'Your holiday request was approved.',
      exampleNotificationCaption: 'You can unsubscribe at anytime.',
      acceptButtonText: "ALLOW",
      cancelButtonText: "NO THANKS"
  }
  }]);


  this.OneSignal.push(function () {
    this.OneSignal.isPushNotificationsEnabled(function (isEnabled) {

      if (!isEnabled) {
        this.OneSignal.push(function () {
          this.OneSignal.registerForPushNotifications({
            modalPrompt: true
          });
        });
      }
      else {
        this.OneSignal.push(function () {
          this.OneSignal.getUserId().then(function (userId) {
            that.oneSignalId = userId;
          });
        });
      }
    });
  });


  this.OneSignal.push(function () {
    this.OneSignal.on('subscriptionChange', function (isSubscribed) {
      if (isSubscribed) {
        this.OneSignal.getUserId().then(function (userId) {
          that.oneSignalId = userId;
        });
      }
      else {
        that.oneSignalId = "";
      }
    });
  });


  this.OneSignal.on('notificationDisplay', function (event) {
    that.notificationDisplayed(event);
  });
}

The modal dialog still appears allowing me to enable notifications but it fails to get the User Id.

Any help appreciated.

Thanks.


#2

Did you instanceid the oneSignal variable in the constructor? Like
constructor(public oneSignal: OneSignal) {}


#3

Is that needed for the One Signal javascript?

I used a variable and grabbed what’s in the index.html,

this.OneSignal = window["OneSignal"];

I removed all the OneSignal native stuff trying to track down the issue.