Good morning, how could I open an activity depending on what is required with Firebase notifications?
Originally the pendingIntent can open a predefined activity but in my case I need to open the notification depending on the type of notification the user receives
For example I want it to go to my reports activity when I'm inside the app but I want it to go to login when I'm outside, I also want it to go to a chat activity when I'm inside and to login when I'm outside which means I would need multiple intents different from the pendingintent, is there a way to do something like that using the same FCM class? I don't know if validating something specific or something like that.
This is my code:
public PendingIntent clicknoti(){
mensaje = 1;
Intent nf = null;
if (Login.cargo == 1){
DatosPrincipales.gIdEstado = 2;
nf = new Intent(getApplicationContext(), ObtenerReportesAdmin.class);
}
else if (Login.cargo == 0 && gIdUsuario > 0) {
obtenerEstado();
nf = new Intent(getApplicationContext(), ObtenerReportes.class);
}
else if (Login.cargo == 0 && gIdUsuario == 0){
nf = new Intent(getApplicationContext(), Login.class);
}
nf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(this, requestCode++, nf,
PendingIntent.FLAG_UPDATE_CURRENT);
}
As you have noticed, I am using my database to validate the intent but now that I need a couple more attempts it no longer works for me because the validation only works for a couple of activities, not for all of them, so I don't know if there is another way to do it or a more feasible one.
Full class:
public class Fcm extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.e("token","mi token es:"+s);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from =remoteMessage.getFrom();
if (remoteMessage.getData().size()>0){
String titulo=remoteMessage.getData().get("titulo");
String detalle=remoteMessage.getData().get("detalle");
mayorqueoreo(titulo,detalle);
}
}
private void mayorqueoreo(String titulo, String detalle) {
String id="mensaje";
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,id);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel nc=new NotificationChannel(id,"nuevo", NotificationManager.IMPORTANCE_HIGH);
nc.setShowBadge(true);
assert nm!=null;
nm.createNotificationChannel(nc);
}
//Nuevas lineas agregadas
Intent notificationIntent = new Intent(getApplicationContext(), ObtenerReportesAdmin.class);
//Trate de agregar otro intent pero solo le puedo pasar 1 parametro a .setContentIntent
Intent notificationIntent2 = new Intent(getApplicationContext(), ObtenerReportesAdmin.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentTitle(titulo)
.setSmallIcon(R.drawable.logotaqueria)
.setContentText(detalle)
.setContentIntent(pendingIntent)
.setContentInfo("nuevo");
Random random=new Random();
int idNotity =random.nextInt(8000);
assert nm !=null;
nm.notify(idNotity,builder.build());
}
public PendingIntent clicknoti(){
//He quitado todo el codido del metodo clicknoti pero me pide un valor de retorno. ¿Debo eliminar este metodo?
}
}
I hope you can help me, thank you very much in advance!
Inside the
PendingIntent
, define theIntent
one you previously created to open theActivity
one you want:You define the PendingIntent to your Notification:
Check these answers:
How do I make an Intent within the body of a notification in android?
PendingIntent in Notification