nuxt-community / onesignal-module

OneSignal for Nuxt 2
MIT License
74 stars 8 forks source link

getting error many times t.$OneSignal.isPushNotificationsEnabled is not a function #4

Open imsidz opened 5 years ago

imsidz commented 5 years ago

Version

v3.0.0-beta.19

Reproduction link

https://github.com/nuxt-community/pwa-module

Steps to reproduce

i install pwa module to my app normaly with npm i @nuxtjs/pwa @nuxtjs/onesignal use this to my nuxt.config.js

modules: [
  '@nuxtjs/onesignal',
  '@nuxtjs/pwa',
],

// Options
oneSignal: {
  init: {
    appId: 'YOUR_APP_ID', // <- my private key
    allowLocalhostAsSecureOrigin: true,
    welcomeNotification: {
        disable: false
    }
  }
}

and added this to my default.vue layout

mounted() {
    this.$OneSignal.push(() => {
        this.$OneSignal.isPushNotificationsEnabled((isEnabled) => {
        if (isEnabled) {
          this.$OneSignal.getUserId( function(userId) {
              console.log('player_id of the subscribed user is : ' + userId);
              // Make a POST call to your server with the user ID        
          });
        } else {
          console.log('Push notifications are not enabled yet.')
        }
      })
    })
  }

What is expected ?

it should return player_id of the subscribed user is : ' + userId

What is actually happening?

t.$OneSignal.isPushNotificationsEnabled is not a function

Additional comments?

i did try to clear cache and refresh it work first time than again return this error

This bug report is available on Nuxt community (#c170)
matheuschimelli commented 5 years ago

Have you ever tryed use with async/await? Take a look at: OneSignal Web SDK API

This is the code provided on docs:

OneSignal.push(function() {
  /* These examples are all valid */
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");    
  });

  OneSignal.isPushNotificationsEnabled().then(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");      
  });
});

I can't test locally right now, but i gonna try later using this code:

// enable async function
this.$OneSignal.push(async()=> {
    this.$OneSignal.isPushNotificationsEnabled((isEnabled)=>{
     if (isEnabled)
       console.log("Push notifications are enabled!");
     else
       console.log("Push notifications are not enabled yet.");    
   });
 //use await
 const isPushEnabled = await this.$OneSignal.isPushNotificationsEnabled()
     if (isPushEnabled){
       console.log("Push notifications are enabled!");
    } else {
       console.log("Push notifications are not enabled yet.");      
 }
   });
buglavecz commented 4 years ago

Hi,

I often get this error too: ReplayCallsOnOneSignal.js:23 TypeError: t.$OneSignal.showNativePrompt is not a function

image

buglavecz commented 4 years ago

Have you ever tryed use with async/await? Take a look at: OneSignal Web SDK API

This is the code provided on docs:

OneSignal.push(function() {
  /* These examples are all valid */
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");    
  });

  OneSignal.isPushNotificationsEnabled().then(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");      
  });
});

I can't test locally right now, but i gonna try later using this code:

// enable async function
this.$OneSignal.push(async()=> {
    this.$OneSignal.isPushNotificationsEnabled((isEnabled)=>{
     if (isEnabled)
       console.log("Push notifications are enabled!");
     else
       console.log("Push notifications are not enabled yet.");    
   });
 //use await
 const isPushEnabled = await this.$OneSignal.isPushNotificationsEnabled()
     if (isPushEnabled){
       console.log("Push notifications are enabled!");
    } else {
       console.log("Push notifications are not enabled yet.");      
 }
   });

@imsidz it is works?

P4sca1 commented 4 years ago

Might be related to nuxt-community/onesignal-module#1 and nuxt-community/onesignal-module#3

xdiegom commented 4 years ago

Have you ever tryed use with async/await? Take a look at: OneSignal Web SDK API This is the code provided on docs:

OneSignal.push(function() {
  /* These examples are all valid */
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");    
  });

  OneSignal.isPushNotificationsEnabled().then(function(isEnabled) {
    if (isEnabled)
      console.log("Push notifications are enabled!");
    else
      console.log("Push notifications are not enabled yet.");      
  });
});

I can't test locally right now, but i gonna try later using this code:

// enable async function
this.$OneSignal.push(async()=> {
    this.$OneSignal.isPushNotificationsEnabled((isEnabled)=>{
     if (isEnabled)
       console.log("Push notifications are enabled!");
     else
       console.log("Push notifications are not enabled yet.");    
   });
 //use await
 const isPushEnabled = await this.$OneSignal.isPushNotificationsEnabled()
     if (isPushEnabled){
       console.log("Push notifications are enabled!");
    } else {
       console.log("Push notifications are not enabled yet.");      
 }
   });

@imsidz it is works?

It worked for me 👍🏼, but I refactored this way because logging messages were duplicated:

this.$OneSignal.push(async () => {
  await this.$OneSignal.isPushNotificationsEnabled((isEnabled) => {
    if (isEnabled) console.log('Push notifications are enabled!')
    else console.log('Push notifications are not enabled yet.')
  })
})

Don't really know if this is correct although OneSignal code docs are provided like that 🤔

Nuxt Version: 2.14.0 @nuxtjs/onesignal: 3.0.0-beta.16 @nuxtjs/pwa: 3.0.0-beta.20

productdevbook commented 3 years ago

Anyone using this library? does it work ? I have the codes giving an error.