FCM notification not working when app is kill/closed

Hello, I have done coding on FCM notification in ionic 3 it works fine when app is running foreground and running background but when app is completely closed/killed no response on FCM notification got so please help on this.I am stuck hear about one week didn’t able to solve this issue.just tell me is there a way to get notification when app is killed?

1 Like

Hi,
Can u share the json format how you are sending when app in BGD/FGD?
this will be the json format for sending JSON

{
  "notification":{
    "title":"Notification title",
    "body":"Notification body",
    "sound":"default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"fcm_push_icon"
   
   
  },
  "data":{
  	"title":"actual data title",
    "body":"actual data body"
   
   
  },
    "to":"your device token",
    "priority":"high"

 
    
}
1 Like

this is my code

//backend coding 
sendnotification(sname, msg) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'key=' + "API_Key");
    let option = new RequestOptions({ headers: headers });
    let body = {
      "notification": {
        "title": sname,
        "body": msg,
        "badge": 1,
        "sound": "default",
        "click_action": "FCM_PLUGIN_ACTIVITY"
      },
      "priority": "high",
      "to": "Device_token",
    }
    this.http.post('https://fcm.googleapis.com/fcm/send', JSON.stringify(body), option)
      .map(res => res.json())
      .subscribe(data => {
        console.log(data);    
      });
  }
//frontend coding
  OnNotification() {  
    FCMPlugin.onNotification(function (data) {
      if (data.wasTapped) {
        //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));
      }
    },
      function (msg) {
        console.log('onNotification callback successfully registered: ' + msg);
      },
      function (err) {
        console.log('Error registering onNotification callback: ' + err);
      });
  }

@yashwanth493 thanks for the reply I get notification when app in BGD/FGD,i want to run my FCM notification when app is completely closed/killed like watsapp, this is my code in this you can see JSON as well.

Thank you,

try with this body

I tried this body but it’s not working

can anyone find the solution?

Is your issue on Android or iOS?

I am facing this issue on android

I haven’t used FCM but if you only need FCM for push notifications then I suggest you use OneSignal. It’s much easier to use and I haven’t encountered issues with it at all, something I can’t say about the old GCM plugin (FCM predecessor) I used previously

I haven’t use OneSignal before so can you tell me how would i get notification using OneSignal in my code(explain it above in my forum) .

thank you

ok thanks i will try this out.

i have been found after almost 3 days.

Ionic Push Notification - Android and iOS working

it’s absolutely working pretty much fine for me.

@nirav_ionic, my notification worked fine my issue is that when i swiped up or kill/close only that time my app unable to get notification from FCM.

Push

just follow his code they working in foreground,background & also in kill mode.

i tried this ,not working. can you share your git/code?

I have code right now with me, but for some reason I unable to share on Github. So, can you please share your code with me and let me check.

i have already shared it please see above.

using https://ionicframework.com/docs/native/push/

initPushNotification() { 

    if (!this.platform.is('cordova')) {
      console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
      return;
    }
    const options: PushOptions = {
      android: {
        senderID: '927955702181',
        sound : 'true',
        vibrate:true,
        forceShow:true,        
      },
      ios: {
        alert: 'true',
        badge: 'true',
        sound: 'true',
        clearBadge: 'true'
      },
      windows: {}
    };
    const pushObject: PushObject = this.push.init(options);

    pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification 11', notification));
    pushObject.on('registration').subscribe((data: any) => {
      console.log('device token -> ' + data.registrationId);
      this.globalvarProvider.setFcmToken(data.registrationId);  
      //TODO - send device token to server
    }); 
     pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
  }
2 Likes