Firebase (Fcm): Open Activity and Pass Data on Notification Click. Android

Firebase (FCM): open activity and pass data on notification click. android

After trying all the answers and blogs came up with solution. if anyone needs please use this video as reference

https://www.youtube.com/watch?v=hi8IPLNq59o

IN ADDITION to the video to add intents do in MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

String user_id = "0";
String date = "0";
String hal_id = "0";
String M_view = "0";

if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
user_id = remoteMessage.getData().get("user_id");
date = remoteMessage.getData().get("date");
cal_id = remoteMessage.getData().get("hal_id");
M_view = remoteMessage.getData().get("M_view");
}

String click_action = remoteMessage.getNotification().getClickAction();

//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), user_id, date, hal_id, M_view, click_action);
}

private void sendNotification(String messageBody, String messageTitle, String user_id, String date, String hal_id, String M_view, String click_action) {
Intent intent = new Intent(click_action);
intent.putExtra("user_id", user_id);
intent.putExtra("date", date);
intent.putExtra("hal_id", hal_id);
intent.putExtra("M_view", M_view);

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}}

and in new NotificationReceive activity in onCreate or onResume add this

    notification_Y_N = (TextView) findViewById(R.id.notification_Y_N);
user_id_text = (TextView) findViewById(R.id.user_id_text);

Intent intent_o = getIntent();
String user_id = intent_o.getStringExtra("user_id");
String date = intent_o.getStringExtra("date");
String hal_id = intent_o.getStringExtra("hal_id");
String M_view = intent_o.getStringExtra("M_view");

notification_Y_N.setText(date);
user_id_text.setText(hal_id);

Open activity after FCM notification click in Android

If app in background, notifications directs to the system tray.

After click notification it will redirect to launcher activity which is given in manifest file.

From launcher activity we can call any activity but first it will goto launcher activity.

In Launcher activity we can get notification message contents .

AndroidManifest.xml

 <activity

android:name=".view.SplashActivity"

android:screenOrientation="portrait">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

SplashActivity.Java

public class SplashActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Bundle bundle = getIntent().getExtras();

if (bundle != null && bundle.get("data")!=null) {

//here can get notification message
String datas = bundle.get("data").toString();

}
}
}

Send data to activity when Firebase notification is clicked in Android

When sending notification from console, add the custom data from 'Advanced options':

Sample Image

For some reason, firebase doesn't allow the key to start with fcm. You cannot use fcm_notification. Use a different key.

For above image, receive the key as follows:

String value = getIntent().getStringExtra("test_key");

Firebase data notification cannot click

I hope you are implementing code for android, so to make notification clickable you just need to add the pendingIntent with destination Activity name so that you will get the clickable action. Use the below code for reference.

 try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}

Intent mainIntent = new Intent(this, TabHostScreen.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent mainPIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(getNotificationIcon(builder));
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setAutoCancel(true);
//start intent on notification tap (MainActivity)
builder.setContentIntent(mainPIntent);
//custom style
builder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
builder.setCustomContentView(remoteCollapsedViews);
//builder.setCustomBigContentView(remoteExpandedViews);
long[] pattern = {500, 500, 500};
builder.setVibrate(pattern);
Random rand = new Random();
NOTIFICATION_ID = rand.nextInt(1000);
//notification manager
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());

How to open specific activity from push notification in Android&FCM?

You will need to use Android Deep linking.

Your notification (FCM) should have a payload of some data (Message ID all depends on your design) for the given screen you want to consume in this case your chat Activity.

The chat Activity will need to have a host and schema declared in the manifest, below is the instructions and code for Deep Linking:

https://developer.android.com/training/app-links/deep-linking

Happy coding.

How to open fragment from the click of the notification?

When user tries to open notification and tries to open the fragment he want to load the data i tried this way and it performed well.. even when app was closed and even when the app is running it is working.

MainActivity

in on create

      Intent intent = getIntent();
if(intent !=null&&intent.getExtras()!=null)
{
id = intent.getExtras().get("id").toString();
type = intent.getExtras().get("type").toString();
//here i check if data is not null then i proceed to pass the data to the fragment using nav controller i defined earlier.
bundle.putString("lessonId", id);
bundle.putString("type", type);
bundle.putLong("lastPosition", Math.round(lastPosition));
navController.navigate(R.id.player, bundle);
}

this works when I try to click the notification when the app is open,
but when the app is not open it doesn't fetch the data from intent
because it isn't the starter app.

so i did the same process in splash activity and got the data from notification from that and i passed that data into main activity from where the fragment is being called.

Thank you so much guys for quick support..!!



Related Topics



Leave a reply



Submit