0

mi consulta es la siguiente, la aplicación tiene un login, un pantalla principal y varias pantallas más, lo que quiero hacer es que el servicio FirebaseMessagingService me muestre una notificación y al hacer click en la notificación me dirija a la pantalla principal. El problema es que cuando esta en primer plano , por cada notificación que toco me abre una nueva app

public class NotificationService extends FirebaseMessagingService {

public NotificationService() {
}

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("FCM",s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String tituloD=remoteMessage.getData().get("TituloD");
    String descripcionD=remoteMessage.getData().get("DescripcionD");
    showNotificacion(tituloD,descripcionD);
    Log.d("FCM",tituloD+"-"+descripcionD);

}


private void showNotificacion(String tituloD,String descripcionD){
    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String CHANNEL_ID = "my_channel_01";
    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "01")
            .setContentTitle(tituloD)
            .setSmallIcon(R.drawable.logo)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(descripcionD))
            .setContentText(descripcionD)
            .setAutoCancel(true)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(pendingIntent)
            .setChannelId(CHANNEL_ID);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {       // For Oreo and greater than it, we required Notification Channel.
        CharSequence name = "My New Channel";                   // The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name, importance); //Create Notification Channel
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(1 , notificationBuilder.build());

}}

Como podría hacer que no se abra una nueva app por cada notificación?.

1 respuesta 1

0

Tu pendinintent es el que define ma intención que se realizará al evento.

El comportamiento de tu aplicación cuando recibe notificación lo vas a tener que gestionar.

Cuando la app está en primer plano deberás generar la notificación de manera local la bandera indicará cual será el comportamiento de la instancia si hay alguna activa. Pero en tu aplicación en el punto de entrada deberás estar escuchando intenciones que lleguen con un flag y por ejemplo que viene de un click en pendintent de esta forma tu redirecciones a la pantalla principal.

En caso contrario que siga el flujo normal de verificar si tiene login o no para presentar la actividad que corresponda.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.