Ionic push notification using fcm?

hi @all,
i am trying to setup push notifications in my ionic 3 app using fcm , but i am unable to find out any proper tutorial for it .Also i am not able to find out register /unregister method in “cordova-plugin-fcm”. as i will register device token , when any user logged in and unregister , when that user logged out.
If anyone can help me in this regard , please help me
Thanks and regards

Please refer the ionic documentation

@azarzaza, i have tried following code:
import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import { FCM } from ‘@ionic-native/fcm’;
@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {

constructor(public navCtrl: NavController,private fcm:FCM) {

}
login(){
try{
this.fcm.getToken().then(token=>{
alert("token is "+token);
})

this.fcm.onNotification().subscribe(data=>{
  alert("notify");
  if(data.wasTapped){
    alert("Received in background");
  } else {
    alert("Received in foreground");
  };
},error =>{
  alert("error is "+JSON.stringify(error));
})

}catch(e){
alert(JSON.stringify(e));
}

}

}
This works fine when app is in foreground.
But when app is in background it doesn’t display any alert in “onNotification” method.please tell, what I am missing here

Moreover , how i can unregister/unsubscribe device from receiving push notifications when user logged out

1 Like

@inder

how i can unregister/unsubscribe device from receiving push notifications when user logged out

you can handle this with topics when user login, user can subscribe the topic and also when logout user can unsubscribe the topic using this function
fcm.subscribeToTopic('user');
fcm.unsubscribeFromTopic('user');

@azarzaza any idea about why notification handler not working when app is in background?

Where you put your notification subscription code ?

@azarzaza in the constructor of app.component.ts

Share your code I can check what goes wrong ?

M also facing d same issue… Background notification not workig… Notification cmng butconsole or alert not showing… Code is same as @inder pasted… @azarzaza

Foreground notification showing in console but not background…

1 Like

this is a proper tutorial for using fcm plugin in ionic and i am also use this tutorial for fmc in ionic and its work for me (both platform ‘android’&‘ios’) https://www.djamware.com/post/58a1378480aca7386754130a/ionic-2-fcm-push-notification

1 Like

It’s good tutorial! Everything is in perfect

Same method i used but code didnt work, m facing same issue @YAsh0011

Send me the payload syntax which you are using from backend… It ll help me i thnk.
Thanks in advance

I don’t have my backend now (sorry), but if not working for you which link i had sent you ,
so you can try this push notification plugin
i hope this code work for you

Hey @YAsh0011 @azarzaza I solved my problem of fcm, the backend syntax were not right.
Now i need to solve twilio chat integeration or fcm chat wth ionic 2,do help me again… Thankx in advance…

Demo of this ll b helpful

You have to add the property "click_ action": "FCM_PLUGIN_ACTIVITY" to the "notification" object in order to properly use the background method.

Example:

{
 "to" : "tokenHere",
 "notification" : {
     "title": "title",
     "body" : "message",
     "sound": "default",
     "click_action":"FCM_PLUGIN_ACTIVITY"
 },
 "data": {
 	"content": "whatever",
 },
 "priority":"high"
}

The alert alert("Received in background"); will show after you click the notification

1 Like

Hello nodejose
Im trying to do a Push Notification with buttons to click_action, but a sent a message to firebase and received this message, but without the button.

Do I need to do something different than I did on my ionic3 project?

in app.components.ts

import { Firebase } from ‘@ionic-native/firebase’;

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, firebase: Firebase) {
platform.ready().then(() => {
firebase.onNotificationOpen().subscribe(data=>{
console.log(data);
console.log(data.name)
}, err=> console.log(err));
});
}

if your app is in background it will not alert till the user click on notification.