Reputation: 101
I am receiving the notification message even if the app is in the background, but whenever a notification is received it automatically opens the activity (even if the user doesn't click on the notification).
My onMessageReceived
is
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle b = new Bundle();
if (data.containsKey("click_action")) {
if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")){
b.putString("PostId", data.get("post_id").toString());
Firebase_Action_Click.startActivity(data.get("click_action"), b, this);
} else {
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
}
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentText(data.get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
And in my Firebase_Action_Click
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);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
And in my launcher activity I have added these lines,
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("click_action")) {
Firebase_Action_Click.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
}
}
Everything goes fine but the only problem I face is even if the app is in the background and I get a notification it automatically opens the application.
I want the same action to be done only when the user clicks on the Notification.
Could anyone help me on this, Thanks in advance
Upvotes: 2
Views: 1470
Reputation: 24211
It fires the Activity
because you launch the Activity
from the onMessageReceived
function. So if you want to launch the Activity
when you click on your Notification you may need to start the Activity
as your PendingIntent
.
I would like to recommend simple modification in your code. Your startActivity
function in Firebase_Action_Click
may return the Intent
it has created and look like this.
public static Intent 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);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return i; // Return the intent from here.
}
Now in your onMessageReceived
function, you need to set a PendingIntent
too inside if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost"))
- this if statement, like the one you set for the else
part. So your code might look like this.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle b = new Bundle();
if (data.containsKey("click_action")) {
if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")) {
b.putString("PostId", data.get("post_id").toString());
Intent intent = Firebase_Action_Click.startActivity(data.get("click_action"), b, this);
// Now add the flags and create pendingIntent here
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
} else {
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentText(data.get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Now your notification system should work fine.
Upvotes: 1