How to receive notification push in Nativescript-vue

294 Views Asked by At

Here is my code in app.js

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]

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

})

// Get the device token
firebase().messaging()
  .getToken()
  .then(token => {
    console.log(token);
  });

firebase()
  .messaging()
  .onMessage(async remoteMessage => {
    console.log('A new FCM message arrived!'+JSON.stringify(remoteMessage));
  });

firebase().messaging().onBackgroundMessage

I want the FCM alarm to work in the background, and when I click the alarm, I want the console.log to show what kind of message I got. But I don't have idea what function I use to. Please Help.

1

There are 1 best solutions below

0
Seoho.An On

I used those codes.

//background notification
    firebase().messaging().onNotificationTap(async message =>{
      var body = message.notification.body;
      this.saveHistory(message.notification.title,body);
      this.$navigateTo(Detail,{
        props:{
          shtUrl : body
        }
      });
firebase().messaging().showNotificationsWhenInForeground = true;
    //foreground notification
    firebase().messaging().onMessage(async remoteMessage => {
      let _this = this;
      this.saveHistory(remoteMessage.notification.title,remoteMessage.notification.body);

      Dialogs.confirm({
        title: remoteMessage.notification.title,
        message: remoteMessage.notification.body,
        okButtonText: "Go",
        cancelButtonText: "No"
      }).then(function (result) {
        if(result){
          _this.$navigateTo(Detail,{
            props:{
              shtUrl : remoteMessage.notification.body
            }
          });
        }
      });
    });