FCMPlugin - how to handle data in "onNotification" function?


#1

Hi guys,

i am using cordova-plugin-fcm and want to handle the data from a push notification. For example if i get a chat message i want to create a toast if i am not in the ChatView or update my chat if i am in the ChatView.

The question:
How can i use e.g. the ToastController in the:

FCMPlugin.onNotification(
function (data) {
if (data.wasTapped) {
} else {
}
},
function (msg) {
console.log('onNotification callback successfully registered: ’ + msg);
},
function (err) {
console.log('Error registering onNotification callback: ’ + err);
}
);

Because if i initialize the ToastController in the constructor of parent class and write “this.toastCtrl” the “this” is for FCMPlugin and not for the parent class.

Kind regards,
Diesi


#2

I used an arrow function for the first callback to access the
constructor(private tc:ToastControllrt){}

FCMPlugin.onNotification(
                        (data) => {
                            console.log('Data RAW: ', data);

                            if (data.wasTapped) {
                                //received background
                                if (data.type == 'status') {
                                 //do stuff
                                    });

                                

                                    let toast = this.toastCtrl.create({
                                        position: 'top',
                                        message: 'message',
                                        duration: 5000,
                                        showCloseButton:true
                                    });
                                    toast.present();
                                }


                            } else {
                                //received foreground
                                if (data.type == 'status') {

                                    let msg = 'some message';
                                    if (data.message) {
                                        msg = data.message;
                                    }

                                    let toast = this.toastCtrl.create({
                                        position: 'top',
                                        message: msg,
                                        duration: 5000,
                                        showCloseButton:true
                                    });
                                    toast.present();


                                }
                            }
                        },
                        function (msg) {
                            console.log('onNotification callback successfully registered: ' + msg);
                        },
                        function (err) {
                            console.log('Error registering onNotification callback: ' + err);
                        }
                    );

#3

Thank you so much - this is working fine.


#4

Hello, I have tried using the arrow key but it doesn’t work for me. Also console log is not giving any information


ionViewDidLoad() {
    FCMPlugin.onNotification(
      (data) => {
        console.log("Data : " , data);
      if (data.wasTapped) {
        this.navCtrl.push('BuddiesPage');
        //Notification was received on device tray and tapped by the user.
        alert(JSON.stringify(data));
 
      } else {
        //Notification was received in foreground. Maybe the user needs to be notified.
        alert(JSON.stringify(data));
        console.log("hello from main menu");

      }
    });
  
    FCMPlugin.onTokenRefresh(function(token) {
      alert(token);
    });
  }