View specific activity from fire base notification

I have included a benefit notification for my application, but I would like to send a notification that opens a specific activity and does what I plan to do, and not just open the application. as a notification that will force the user to visit the Google Play store when you click on it.

I saw the Firebase console code : how to specify click_action for notifications , which I used, but I get an error to initialize cls variables. I tried to solve by defining cls = null to clear the error. It cannot open my specified action using click_action

public class ClickActionHelper { public static void startActivity(String className, Bundle extras, Context context){ Class cls=null; try { cls = Class.forName(className); } catch(ClassNotFoundException e){ //means you made a wrong input in firebase console } Intent i = new Intent(context, cls); i.putExtras(extras); context.startActivity(i); } } 

Am I getting something wrong? How can I make this work?

+2
source share
2 answers

If you want to open the application and perform a specific action [in the background], set click_action in the notification payload and map it to the intent filter in the action you want to run. For example, set click_action to OPEN_ACTIVITY_1 to trigger the intent filter as follows:

As stated in the FCM docs, ask the backend to send JSON data in a form like this,

  { "to":"some_device_token", "content_available": true, "notification": { "title": "hello", "body": "yo", "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity }, "data": { "extra":"juice" } } 

and in your mainfest file add an intent filter for your activity as below

 <intent-filter> <action android:name="OPEN_ACTIVITY_1" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 

When you click on the notification, it will open the application and go to the action that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside this activity you can get data:

 Bundle b = getIntent().getExtras();// add these lines of code to get data from notification String someData = b.getString("someData"); 

Check out the links below for more help:

Firebase FCM click_action download notification

Firebase onMessageReceived is not called when the application is in the background

Firebase console: how to specify click_action for notifications

+7
source

I know this question has existed for some time, but I still wanted to show my solution. It is much simpler than those presented. so now i wander if its a bad practice. but it works: I use the payload jload object to store an integer:

 JSONObject payload = data.getJSONObject("payload"); int destination = payload.getInt("click_action"); 

then I just use a simple switch statement to trigger the right action based on an integer result:

 Intent resultIntent; switch(destination){ case 1: resultIntent = new Intent(getApplicationContext(), UserProfileActivity.class); resultIntent.putExtra("message", message); break; default: resultIntent = new Intent(getApplicationContext(), MainActivity.class); resultIntent.putExtra("message", message); break; } 

Simple

0
source

Source: https://habr.com/ru/post/1257467/


All Articles