Error in notifications android studio does not raise activity

3

My problem is as follows The notifications arrive well when the app is open, but when it is in the background they do not arrive well, it does not raise the activity in the notification. The project is in Pie the sdk and compilation

This the service code:

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>
    
asked by Diego 27.10.2018 в 18:10
source

1 answer

2

Hello problem you have is that you are using the same request code for 3 different notifications.

The best option would be to use a different request code for each one a serious example.

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();
                }
            }
        });

    }
}
    
answered by 29.10.2018 в 02:43