3

Mi problema es el siguiente Las notificaciones llegan bien cuando la app esta abierta, pero cuando esta en segundo plano no llegan bien no levanta la actividad en la notificación. El proyecto esta en Pie el sdk y compilación

Este el código del servicio:

public class MiFirebaseMessagingService extends FirebaseMessagingService {

public static final String TAG = "NOTICIAS_Servicio";

private PendingIntent pendingIntent;
public final static String CHANNEL_ID = "NOTIFICACION_REFRESHAPP";
public final static int NOTIFICACION_ID = 1001;

private String type;
private String id;
private String title;
private String body;

FirebaseFirestore db = FirebaseFirestore.getInstance();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String from = remoteMessage.getFrom();
    Log.d(TAG, "Mensaje recibido de: " + from);

    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Notificación: " + remoteMessage.getNotification().getTitle()+", "+remoteMessage.getNotification().getBody());
        title   =remoteMessage.getNotification().getTitle();
        body    =remoteMessage.getNotification().getBody();
    }

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Data: " + remoteMessage.getData());

        type=remoteMessage.getData().get("type");
        id=remoteMessage.getData().get("id");

        getObject();

    }
}

private void setPendingIntentActividad(Actividad actividad){
    Intent intent = new Intent(this, DetailsActividadActivity.class);
    intent.putExtra(DetailsActividadActivity.ACTIVITY_ACTIVIDAD,actividad);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(DetailsActividadActivity.class);
    stackBuilder.addNextIntent(intent);
    pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void setPendingIntentGrupo(Grupo grupo){
    Intent intent = new Intent(this, DetailsGrupoActivity.class);
    intent.putExtra(DetailsGrupoActivity.ACTIVITY_GRUPO,grupo);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(DetailsGrupoActivity.class);
    stackBuilder.addNextIntent(intent);
    pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void setPendingIntentNoticia(Noticia noticia){
    Intent intent = new Intent(this, DetailsNoticiaActivity.class);
    intent.putExtra(DetailsNoticiaActivity.ACTIVITY_NOTICIA,noticia);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(DetailsNoticiaActivity.class);
    stackBuilder.addNextIntent(intent);
    pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void createNotificationChannel(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        CharSequence name = "Noticacion_App";
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

private void createNotification(){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle(title);
    builder.setContentText(body);
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setLights(Color.MAGENTA, 1000, 1000);
    builder.setVibrate(new long[]{1000,1000,1000,1000,1000});
    builder.setDefaults(Notification.DEFAULT_SOUND);

    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(Aplicacion.getInstance());
    notificationManagerCompat.notify(NOTIFICACION_ID, builder.build());
}

private void getObject(){

    DocumentReference docRef = db.collection(type).document(id);

    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            if(type.equals("actividades")){
                Actividad actividad = documentSnapshot.toObject(Actividad.class);
                setPendingIntentActividad(actividad);
                createNotificationChannel();
                createNotification();
            }else if(type.equals("grupos")){
                Item item = documentSnapshot.toObject(Grupo.class);
                setPendingIntentGrupo((Grupo) item);
                createNotificationChannel();
                createNotification();
            }else if(type.equals("noticias")){
                Noticia noticia = documentSnapshot.toObject(Noticia.class);
                setPendingIntentNoticia(noticia);
                createNotificationChannel();
                createNotification();
            }
        }
    });

}

}

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".Aplicacion">


    <activity android:name=".ui.activity.SplashScreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".MainActivity"/>

    <activity android:name=".ui.activity.DetailsNoticiaActivity"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>

    <activity android:name=".ui.activity.DetailsGrupoActivity"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>

    <activity android:name=".ui.activity.DetailsActividadActivity"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>

    <service
        android:name=".ServiceFCM.MiFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name=".ServiceFCM.MiFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>


</application>

1 respuesta 1

2

Hola problema que tienes es que estas utilizando el mismo request code para 3 diferentes notificaciones.

La mejor opcion seria utilizar uno diferente request code para cada uno un ejemplo seria.

public static final int ID_NOTIFICATION_ACTIVIDAD = 1;
public static final int ID_NOTIFICATION_GRUPO = 2;
public static final int ID_NOTIFICATION_NOTICIA = 3;

public class MiFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = "NOTICIAS_Servicio";

    public static final int ID_NOTIFICATION_ACTIVIDAD = 1;
    public static final int ID_NOTIFICATION_GRUPO = 2;
    public static final int ID_NOTIFICATION_NOTICIA = 3;

    private PendingIntent pendingIntent;
    public final static String CHANNEL_ID = "NOTIFICACION_REFRESHAPP";
    public final static int NOTIFICACION_ID = 1001;

    private String type;
    private String id;
    private String title;
    private String body;

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String from = remoteMessage.getFrom();
        Log.d(TAG, "Mensaje recibido de: " + from);

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Notificación: " + remoteMessage.getNotification().getTitle()+", "+remoteMessage.getNotification().getBody());
            title   =remoteMessage.getNotification().getTitle();
            body    =remoteMessage.getNotification().getBody();
        }

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Data: " + remoteMessage.getData());

            type=remoteMessage.getData().get("type");
            id=remoteMessage.getData().get("id");

            getObject();

        }
    }

    private void setPendingIntentActividad(Actividad actividad){
        Intent intent = new Intent(this, DetailsActividadActivity.class);
        intent.putExtra(DetailsActividadActivity.ACTIVITY_ACTIVIDAD,actividad);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(DetailsActividadActivity.class);
        stackBuilder.addNextIntent(intent);
        pendingIntent = stackBuilder.getPendingIntent(ID_NOTIFICATION_ACTIVIDAD, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    private void setPendingIntentGrupo(Grupo grupo){
        Intent intent = new Intent(this, DetailsGrupoActivity.class);
        intent.putExtra(DetailsGrupoActivity.ACTIVITY_GRUPO,grupo);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(DetailsGrupoActivity.class);
        stackBuilder.addNextIntent(intent);
        pendingIntent = stackBuilder.getPendingIntent(ID_NOTIFICATION_GRUPO, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    private void setPendingIntentNoticia(Noticia noticia){
        Intent intent = new Intent(this, DetailsNoticiaActivity.class);
        intent.putExtra(DetailsNoticiaActivity.ACTIVITY_NOTICIA,noticia);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(DetailsNoticiaActivity.class);
        stackBuilder.addNextIntent(intent);
        pendingIntent = stackBuilder.getPendingIntent(ID_NOTIFICATION_NOTICIA, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    private void createNotificationChannel(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            CharSequence name = "Noticacion_App";
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    private void createNotification(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle(title);
        builder.setContentText(body);
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setLights(Color.MAGENTA, 1000, 1000);
        builder.setVibrate(new long[]{1000,1000,1000,1000,1000});
        builder.setDefaults(Notification.DEFAULT_SOUND);

        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(Aplicacion.getInstance());
        notificationManagerCompat.notify(NOTIFICACION_ID, builder.build());
    }

    private void getObject(){

        DocumentReference docRef = db.collection(type).document(id);

        docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if(type.equals("actividades")){
                    Actividad actividad = documentSnapshot.toObject(Actividad.class);
                    setPendingIntentActividad(actividad);
                    createNotificationChannel();
                    createNotification();
                }else if(type.equals("grupos")){
                    Item item = documentSnapshot.toObject(Grupo.class);
                    setPendingIntentGrupo((Grupo) item);
                    createNotificationChannel();
                    createNotification();
                }else if(type.equals("noticias")){
                    Noticia noticia = documentSnapshot.toObject(Noticia.class);
                    setPendingIntentNoticia(noticia);
                    createNotificationChannel();
                    createNotification();
                }
            }
        });

    }
}

Tu Respuesta

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

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