'Ionic push notification does not open the background app on android

If the notification is clicked the application will be opened only if it is was not on the background and if the app is on the background it will not be brought to foreground. Android platform.



Solution 1:[1]

So I spent a lot of time finding the bug changing parameters and it turned out that<preference name="AndroidLaunchMode" value="singleInstance"/> parameter at config.xml was causing the problem. We used this parameter because deep links were creating new instances of the app. But for now we will ignore that problem.

Solution 2:[2]

Had the same issue but from other reason. The app was not opened when the notification was clicked.

The problem was that I changed the plugin from cordova-plugin-fcm to cordova-plugin-firebase

And the server that send the notification set the click_action to FCM_PLUGIN_ACTIVITY

So I changed (php):

    $n = new Notification($title, $body);
    return $n->setClickAction("FCM_PLUGIN_ACTIVITY")->setIcon('fcm_push_icon')->setSound('default');

To:

    return $n->setIcon('fcm_push_icon')->setSound('default');

Solution 3:[3]

add this line in confix.xml

<preference name="AndroidLaunchMode" value="singleTop" />

and build app, works fine for me...

Solution 4:[4]

<preference name="AndroidLaunchMode" value="singleTop" />

When you add this line in config.xml even then two cases can happen or these cases might also be happening without adding this line

  1. you won't be able to receive notification when app is in background or killed

  2. if you received background notification even then when you click on it. It won't open the respective application

So to solve first point, you should send the notification key whose value is object in object of fcm from backend service

So to solve second point, "click_action" key should not be passed in notification object

So your overall FCM object which is passed in fcm api should look like

{
     "notification": {
         "title": "Test Title",
         "body": "message",
         "sound": "default",
         "icon": "fcm_push_icon",
         "image": ""
     },
     "data": {
         "action": "TEST"
     },
     "to":"caRvu_MeTN6HbMZKHfOO7y:APA91bEOEOAhpZNrDcDWew0uXM1HfKavyXbf3q4U090AHW5FmD7kDDxs4BRquaM*******************************************",
     "priority": "high"
}

NOTE: This thing is only tested for Capacitor Fcm Plugin

Hope this will help you or somebody else :-)

Thanks!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yernarun
Solution 2 michaelbn
Solution 3 PRATHYUSH P
Solution 4 Aman Kumar Gupta