Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open a specific activity from firebase notification

I integrated firebase notification to my application but I would like to send a notification that opens a specific activity and does what I schedule to do, not just opening the App. like a notification that will push the user to visit Google play store on clicking it.

I saw a code Firebase console: How to specify click_action for notifications which I used but am getting an error to initialize variable cls. I tried to resolve by defining cls=null, to clear error. It fails to open my specified activity using the 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); 
    }     
}

please am I getting anything wrong? How do I get this to work?

like image 919
Cistem Avatar asked Sep 23 '16 14:09

Cistem


People also ask

How do I get data from Firebase notification?

A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

What is Firebasemessagingservice?

Firebase Cloud Messaging Platform (formerly named as GCM) is a free mobile notification service by Google that enables (third-party) app developers to send notifications from GCM (Google Cloud Messaging) servers to their users.


2 Answers

If you want to open your app and perform a specific action [while backgrounded], set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following:

As suggested in FCM docs, ask backend to send JSON data in the 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 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 the notification, it will open the app and go straight to activity that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside that activity you can get the data by :

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

Check out below links for more help:

Firebase FCM notifications click_action payload

Firebase onMessageReceived not called when app in background

Firebase console: How to specify click_action for notifications

like image 53
Rissmon Suresh Avatar answered Oct 19 '22 21:10

Rissmon Suresh


I know this question has been around for a while, but I wanted to show my solution anyway. its much simpler than those presented. so now I'm wandering if its bad practice. but it works: I use the payload json 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 launch the right activity based on the 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.

like image 1
crushman Avatar answered Oct 19 '22 19:10

crushman