EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 444 forks source link

messaging.getCurrentPushToken() is returning undefined #1489

Closed satyak450 closed 4 years ago

satyak450 commented 4 years ago

I'm running a Nativescript-Vue App.

Here is my app.js:


import TabController from "./components/TabController";
import Login from "./components/Login.vue";
const appSettings = require("application-settings");
import { configureOAuthProviders } from "./helpers/auth_helper";

require("nativescript-plugin-firebase");
import { messaging, Message } from "nativescript-plugin-firebase/messaging";

messaging.registerForPushNotifications({
  onPushTokenReceivedCallback: (token) => {
    console.log("Firebase plugin received a push token: " + token);
  },

  onMessageReceivedCallback: (message) => {
    console.log("Push message received: " + message.title);
  },

  // Whether you want this plugin to automatically display the notifications or just notify the callback. Currently used on iOS only. Default true.
  showNotifications: true,

  // Whether you want this plugin to always handle the notifications when the app is in foreground. Currently used on iOS only. Default false.
  showNotificationsWhenInForeground: true
})

configureOAuthProviders();

new Vue({
  render: h =>  h('Frame', [h(appSettings.getString('token') ? TabController : Login)])
}).$start();

Here is where I'm calling messaging.getCurrentPushToken():

    <Page class="page">
        <StackLayout>
            <Label id="login" text="Login" textAlignment="center" />
            <TextField class="input" hint="Enter your email" returnKeyType="send" v-model="email"></TextField>
            <TextField class="input" hint="Enter your password" type="password" 
                returnKeyType="send" secure="true" v-model="password"></TextField>
            <Button id="submit" text="Login" @tap="onButtonTap" />
            <FlexboxLayout justifyContent="center" alignContent="center" id="googleContainer" @tap="signInWithGoogle">
                <Image src="~/components/pictures/GoogleLogo.png" id="googleLogo" />
                <Label text="Log In With Google" />
            </FlexboxLayout>
        </StackLayout>
    </Page>
</template>

<script>
    import TabController from "./TabController";
    import axios from "axios";
    import { tnsOAuthLogin } from '../helpers/auth_helper';
    const appSettings = require("application-settings");
    import { messaging } from "nativescript-plugin-firebase/messaging";

    export default{
        data() {
            return {
                email: null,
                password: null,
                tabcontroller: TabController
            }
        },
        methods: {
            onButtonTap: function() {

                messaging.getCurrentPushToken()
                .then(deviceToken => {
                    console.log(`Current push token: ${deviceToken}`)
                    axios.post('sampleurlhere', {
                        email: this.email,
                        password: this.password,
                        deviceID: deviceToken
                    }).then((response) => {

                       //boilerplate
                    }).catch((err) => {

                        //boilerplate
                    })
                }).catch((err) => {
                    console.log(err);
                });

            },
            signInWithGoogle: function() {

                tnsOAuthLogin('google', (response) => {

                    if (response) {
                        this.$navigateTo(TabController, {clearHistory: true});
                    }
                })
            }
        },
        created() {
            console.log(`Notifications enabled? ${messaging.areNotificationsEnabled()}`);
            messaging.getCurrentPushToken(deviceToken => {
                console.log(`Device token here: ${deviceToken}`);
            })
        }
    }
</script>

Whenever this Login page loads, I get device token as undefined, even though it has worked in this exact form before. I'm not sure what to do. Thanks for any and all feedback

satyak450 commented 4 years ago

I figured out that another plugin (nativescript-oauth2) was interfering with firebase registering for push notifications. I'm not sure why or how, but it is

EddyVerbruggen commented 4 years ago

If your issue is with iOS only then it's probably an "app delegate" conflict. Do you need both (what for)?

satyak450 commented 4 years ago

I was using nativescript-plugin-firebase for external client push notifications, and nativescript-oauth2 for sign in with google, I couldn't really understand the documentation for sign in with google for the firebase plugin

EddyVerbruggen commented 4 years ago

@satyak450 There's an example of Google Sign In in the /demo folder.