FCM Push notification redirect/navigate not working when App Killed

Hi All,

I am using Push notification with Firebase using cordova-plugin-fcm-with-dependecy-updated taking examples from documentation https://ionicframework.com/docs/native/fcm

I have also created Rest API using Firebase. Below is the code for rest API

$msg = array
		(
			'body' 	=> "Body Message",
			'title'	=> "Title",
			'icon'	=> 'myicon',/*Default Icon*/
			'sound' => 'mySound',/*Default sound*/
		       'click_action' => 'FCM_PLUGIN_ACTIVITY'		
		);
		
		$data1=array
		(
			'dataorderid' => 'Order Number'
		 );
		
		
		$fields = array
		(
			'registration_ids' => 'FCM Token',
			'notification'	=> $msg,
			'data' => $data1
		);
		
		$headers = array
		(
			'Authorization: key=' . API_ACCESS_KEY,
			'Content-Type: application/json'
		);
#Send Reponse To FireBase Server	
		$ch = curl_init();
		curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
		curl_setopt( $ch,CURLOPT_POST, true );
		curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
		curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
		curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
		print_r(curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ));
		$result = curl_exec($ch );
		curl_close( $ch );

Above code is working fine and sending notification to APP. However, when app is killed and i tap on notification message it does NOT redirect to the intended screen.
It is working fine and redirecting to intended screen if APP is in background or foreground.

Below is the code in my app.component.ts where it’s subscribing.

initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
	  this.fcm.getToken().then(token => {
		//sending to fcm
	  });
	  
	this.fcm.onNotification().subscribe(data => {

    if (data.wasTapped) {

      console.log('Received in background');
	 this.router.navigate(['/location',{locationdata:data.dataorderid}],{ replaceUrl: true });

    } else {

         console.log('Received in foreground');
	 this.router.navigate(['/location',{locationdata:data.dataorderid}],{ replaceUrl: true });
    }

  });  
	  
	  
    });
  }

Only problem, i am facing is that when App is killed and receive notification, i tap on that notification, it’s just opening the App and showing home screen but it has to navigate to some other screen. When app is closed in background or in foreground, its working perfectly.
What can be the problem here? Please help.

Below is my Ionic’s environment details.

Ionic:

   ionic (Ionic CLI)             : 4.11.0 (C:\Users\USERNAME\AppData\Roaming\nvm\v10.16.3\node_modules\ionic)
   Ionic Framework               : @ionic/angular 5.3.2
   @angular-devkit/build-angular : 0.1000.8
   @angular-devkit/schematics    : 10.0.8
   @angular/cli                  : 10.0.8
   @ionic/angular-toolkit        : 2.3.3

Cordova:

   cordova (Cordova CLI) : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms     : android 8.1.0, ios 5.1.1
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.2.1, (and 6 other plugins)

System:

   NodeJS : v10.16.3 (C:\Program Files\nodejs\node.exe)
   npm    : 6.9.0
   OS     : Windows 7

Please guide or help me to sort out this problem App navigate to intended screen even it’s killed.

Thanks,
Zack

1 Like

May be you should research on how to send a payload in the message to a closed app and then subsequently how to handle or process the payload which could even be url in your app.

Check out the Firebase docs for JS:
Receive messages in a JavaScript client
About FCM messages

Because data messages don’t support fcm_options.link , you are recommended to add a notification payload to all data messages. Alternatively, you can handle notifications using the service worker.

Using cordova-plugin-dependency-updated i added this code and it works fine:

// Retrieves the message that, on tap, opened the app.And null, if the app was open normally.

this.fcm.getInitialPushPayload().then((res) => {
       console.log(res);
})

You can change the code for your routing need.

3 Likes

As long as the ‘click_action’ => ‘FCM_PLUGIN_ACTIVITY’ variable is set under the notification grouping in your FCM message when sending the getInitialPushPayload() works

Thanks Daleffe

1 Like

I am sorry but cannot understand anything… I am doing it on Ionic 5 and it is not working at all, anyone can bring me a piece of code that works? thanks :wink:

Does anyone can write here a JSON that works for iOS and Android please?

Thank you

also would like if it is possible, to see one functional code to get the messages on iOS/Android (both)

Nice, this work for me <3

1 Like

This works for me fine. The thing is that If I use postman to send notification it works. If i use firebase console it didn’t work. I think it may be for "click_action":"FCM_PLUGIN_ACTIVITY", this. because in firebase console there is no option to add this.

did you find a solution? I have the same problem