FCM notification not working when app is kill/closed

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

@nirav_ionic, i know native Push working in all condition but in push i cannot able to open the app when i click on notification so can you help me on this?

which plugin use FCM or Push?

In push plugin click action is not working.

Can anyone find any solution?

@nirav_ionic thank you so much finally i found the solution using https://ionicframework.com/docs/native/push/.

always welcome

please mark as solution so easily identified by other

Just need to implement forceStart:“1” in JSON format and app will be awake from closed app so finally this is my final code with REST API

<===== REST API =====>

 let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'key=' + "**SERVER KEY**");
    let option = new RequestOptions({ headers: headers });
    let body = {
      "data": {
        "title": "TITLE",
        "body": "MSG",
        "forceStart": "1"
      },
      "priority": "high",
      "to": **TOKEN**
    }
    this.http.post('https://fcm.googleapis.com/fcm/send', JSON.stringify(body), option)
      .map(res => res.json())
      .subscribe(data => {
        console.log(data);
        this.autostart.enable();
      });
  }
<===== PusH plugin ====>
const options: PushOptions = {
			android: {
				senderID: '**APP ID**',
				sound: 'true',
				vibrate: true,
				forceShow: "1",
			},
			ios: {
				alert: 'true',
				badge: 'true',
				sound: 'true',
				clearBadge: 'true'
			},
			windows: {}
		};
		const pushObject: PushObject = this.push.init(options);

		pushObject.on('notification').subscribe((notification: any) => {**foreground and background coding**});
		pushObject.on('registration').subscribe((data: any) => {
			console.log('device token -> ' + data.registrationId);			
		});
		pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));


	}
5 Likes

Hi Muji,
Could you explain how do you do with OneSignal to send/receive notifications when app is closed/killed ?