nativescript-vue

Use NativeScript-Vue.js-Firebase for App push


I write app.js like this.

import Vue from 'nativescript-vue'
import Home from './components/Home'

new Vue({
  render: (h) => h('frame', [h(Home)]),
}).$start()



import { firebase } from '@nativescript/firebase-core'
import '@nativescript/firebase-auth'; // only needs to be imported 1x
import '@nativescript/firebase-storage';
import '@nativescript/firebase-messaging';

firebase().initializeApp().then(fbApp => {
  console.log("Firebase app initialized!", fbApp.name) //[DEFAULT]
  console.log('12');
  console.log('fpApp : ',fpApp.auth);

  firebase().auth().addAuthStateChangeListener(async (user) => {
    if (!user) {
      console.log("firebase.auth done");
    } else {
      console.log("firebase.auth else done");
    }
  })

})

I want to return token from handphone, but I don't have idea how to use return code. I found below code. I don't know how to change it to fit my code.

Please Help.

onMessageReceivedCallback: (message) => {
    console.log("[Firebase] onMessageReceivedCallback:", { message });
  },
    onPushTokenReceivedCallback: (token) => {
    console.log("[Firebase] onPushTokenReceivedCallback:", { token });
  },

Solution

  • I solved this use below code.

    firebase().messaging()
      .getToken()
      .then(token => {
        console.log(token);
        return saveTokenToDatabase(token);
      });