4

I'm going to send push notification with custom click action to set onClick of notification to open an exact Activity in my project when the app is in foreground or background. In all posts and comments it is written that it is impossible to send notification with custom click action from console, but in "Advanced options" in console, it is possible to set custom data keys and values.

Firebase Console

Why it should be impossible to set click_action key and its value?

If it is possible so tell me what should I do. I mean what is the exact intent-filter code? and what should I set as click_action value?

Assume that I want to open SecondActivity by clicking on notification.

Mike Burton
  • 3,010
  • 24
  • 33
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
  • 1
    Possible duplicate of [Firebase FCM notifications click\_action payload](https://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload) – AL. Jul 10 '17 at 11:45

1 Answers1

3

yes you can't send notification like this from the firebase console but this is how it works you need to add intent_filter to the activity at the manifest.xml

<activity android:name=".MyActivity">
    <intent-filter>
        <action android:name="com.mypackage.MyActivty_TARGET" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

and put your click_action value as the activity action : com.mypackage.MyActivty_TARGET"

and you read the data from the android like this :

String click_action = remoteMessage.getNotification().getClickAction();
Intent in = new Intent(click_action);
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • What's your idea about [Micha F](https://stackoverflow.com/a/38149137/6444297)'s solution? _the answer that was accepted by the author of this question just states that it is not possible with Firebase Console, but it is - with an easy workaround,_ **Micha Said** – Alireza Noorali Jul 11 '17 at 09:51
  • This solution will never working when sent notification on fcm console when app is background or killed. Because with notification, fcm auto display the notification without export to trigger in the `onMessageReceived` method. So you cannot do this ```String click_action = remoteMessage.getNotification().getClickAction(); Intent in = new Intent(click_action);``` – Sinh Phan Jan 21 '20 at 14:16
  • @PhanSinh and what can you do to handle the click for push notification when the app is in background then? Is this possible to send data to the exact activity you want to open? – Skizo-ozᴉʞS ツ Aug 14 '20 at 14:37
  • No, it is not how the Firebase console works. The click_action provided in the console will not be part of the notification object and it can not be catch with a getClickAction(). – LegZ Mar 06 '21 at 21:39