0

ich bin mit ionischen 2 mit cordova-plugin-fcm Plugin von https://github.com/fechanique/cordova-plugin-fcmWert von Token null ist zwischen Funktionen

i Problem ist vor Wert von Token an den deviceId Zuordnung, zeigt die console.log den Wert korrekt innerhalb der nennen, aber den Wert null zurück außerhalb

der Code ist wie folgt:

declare var FCMPlugin; 

@Component({ 
    templateUrl: `app.template.html` 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 
    public deviceId: any; 


    appPages: PageObj[] = [ 
    { title: 'Home', component: HomePage, icon: 'home' }, 
    { title: 'Profile', component: UserProfilePage, icon: 'calendar' }, 
    { title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' }, 
    ]; 

    rootPage: any; 
    TutorialPage: TutorialPage; 

    constructor(platform: Platform, 
    public menu: MenuController, 
    private actionSheetCtrl: ActionSheetController, 
    public authService: AuthService, 
    public dataService: DataService) { 

    platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 

     FCMPlugin.getToken(
     function (token) { 
      console.log('device token is ' + token); 
      var deviceId = token; 
      console.log('value of id is' + deviceId); 
     }, 
     function (err) { 
      console.log('error retrieving token: ' + err); 
     } 
    ); 
    }); 
    } 

    ngOnInit() { 
    var self = this; 
    this.authService.onAuthStateChanged(function (user) { 
     if (user === null) { 
     self.nav.setRoot(TutorialPage); 
     } 
    }); 
    } 

    ngAfterViewInit() { 
     var self = this; 
    this.authService.onAuthStateChanged(function (user) { 
     if (user === null) { 
     self.menu.close(); 
     self.nav.setRoot(TutorialPage); 
     } else { 
     console.log("device token upon login is" + this.deviceId); 
     let uid = self.authService.getLoggedInUser().uid; 
     console.log("uid at login is " + uid); 
     self.dataService.setDeviceToken(uid, this.deviceId); 
     } 
    }); 

    } 

Werte deviceId im ngAfterViewInit null angezeigt

schließlich arbeiten: ----

bearbeitet Code, der funktioniert, ist:

declare var FCMPlugin; 

@Component({ 
    templateUrl: `app.template.html` 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 
    public deviceId: any; 


    appPages: PageObj[] = [ 
    { title: 'Home', component: HomePage, icon: 'home' }, 
    { title: 'Profile', component: UserProfilePage, icon: 'calendar' }, 
    { title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' }, 
    ]; 

    rootPage: any; 
    TutorialPage: TutorialPage; 

    constructor(platform: Platform, 
    public menu: MenuController, 
    private actionSheetCtrl: ActionSheetController, 
    public authService: AuthService, 
    public dataService: DataService) { 

    platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 

     FCMPlugin.getToken(
     token => { 
      var self = this; 
      console.log('device token is ' + token); 
      let devicetoken = token; 
      console.log('value of id is' + devicetoken); 
      self.deviceId = devicetoken; 

     }, 
     function (err) { 
      console.log('error retrieving token: ' + err); 
     } 
    ); 
    }); 
    } 

    ngOnInit() { 
    var self = this; 
    this.authService.onAuthStateChanged(function (user) { 
     if (user === null) { 
     self.nav.setRoot(TutorialPage); 
     } 
    }); 
    } 

    ngAfterViewInit() { 
    var self = this; 
    this.authService.onAuthStateChanged(function (user) { 
     if (user === null) { 
     self.menu.close(); 
     self.nav.setRoot(TutorialPage); 
     } else { 
     console.log("device token upon login is" + self.deviceId); 
     let uid = self.authService.getLoggedInUser().uid; 
     console.log("uid at login is " + uid); 
     self.dataService.setDeviceToken(uid, self.deviceId); 
     } 
    }); 

    } 

Antwort

0

Der erste Einsatz von 'deviceId' Variable ist eine lokale Variable in der Funktion deklariert.

Die zweite Verwendung von 'deviceId' ist ein Feld in der MyApp-Instanz (Objekt) und wird nie zugewiesen.

Wechseln zu:

FCMPlugin.getToken(
    token => { 
     console.log('device token is ' + token); 
     var deviceId = token; 
     console.log('value of id is' + deviceId); 
     this.deviceId = token; 
    }, 
    function (err) { 
     console.log('error retrieving token: ' + err); 
    } 
); 

Auch stellen Sie sicher, dass Sie die deviceId von der Instanz abzurufen, so in Callback-Funktion onAuthStateChanged, Verwendung self.deviceId statt this.deviceId

+0

nicht seine nicht funktioniert Fehler angezeigt wird seine Darstellung, this.deviceId undefined uid und Geräte Token setdevice Token werden, ist NUBeUq2l7SPNA7xJ7xd7tLKexmB3> undefined – dhruv

+0

es Token „Gerät Token bei der Anmeldung isundefined“ zeigen sollte – dhruv

+0

oh ja, ich denke, das Problem ist, dass Sie verwenden „this“ in die Funktion ... also nochmal das fixe verwenden das du selbst behältst (e..g ersetzen this.deviceId mit self.deviceId) –

Verwandte Themen