stewones / capacitor-fcm-demo

Capacitor FCM Demo
https://github.com/capacitor-community/fcm/tree/master/example
5 stars 1 forks source link

Cannot read property 'getToken' of undefined #2

Open dani12817 opened 4 years ago

dani12817 commented 4 years ago

I'm trying this demo you made, but I'm always getting this error when I try to get the FCM Token, I can only get the Token if I use 'PushNotifications.addListener('registration')'.

zparallax commented 4 years ago

My fix...

      // Push notification settings...
      PushNotifications.register().then(() => console.log(`registered for push`));
      // Select platform
      if (this.platform.is('android')) {
        PushNotifications.addListener('registration',
            (token: PushNotificationToken) => {
              console.log('Push registration success, token: ' + token.value);
            }
        );
      } else {
        PushNotifications.addListener('registration', () => {
          console.log('Registered...');
        });
        // Get FCM token instead the APN one returned by Capacitor
        fcm
            .getToken()
            .then(r => console.log(`Token ${r.token}`))
            .catch(err => console.log(err));
      }

      PushNotifications.addListener(
          'pushNotificationReceived',
          (notification: PushNotification) => {
            console.log(notification);
          }
      );