Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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><actionandroid:name="OPEN_ACTIVITY_1" /><categoryandroid: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 :

Bundleb= getIntent().getExtras();// add these lines of code to get data from notificationStringsomeData= 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

Solution 2:

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 :

JSONObjectpayload= data.getJSONObject("payload");
intdestination= 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){
    case1:
        resultIntent = newIntent(getApplicationContext(), UserProfileActivity.class);
        resultIntent.putExtra("message", message);
        break;
    default:
        resultIntent = newIntent(getApplicationContext(), MainActivity.class);
        resultIntent.putExtra("message", message);
        break;
}

Simple.

Solution 3:

<activity android:name=".design.NotificationActivity"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true"
        android:parentActivityName=".MainActivity"
        >
        <intent-filter>
            <action android:name="NotificationActivity" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>

        $ url = "https://fcm.googleapis.com/fcm/send";
        $token = "";
        $serverKey = '';
        $title = "your title";
        $body = "mesaage";
        $notification = array('title' =>$title ,
        'body' => $body, 
        'sound' => 'default',
        'badge' => '1',
        'click_action' => 'NotificationActivity'
        );
        $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
        $json = json_encode($arrayToSend);
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Authorization: key='. $serverKey;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        //Send the request$response = curl_exec($ ch);
        //Close requestif ($response === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch)

Post a Comment for "Open A Specific Activity From Firebase Notification"