1

Tengo creado una app que recibe una notificación con firebase, recibe esta notificacion si el telefono tiene la pantalla apagada o la app no esta visualizandose en primer plano, si es asi no se visualiza, como puedo hacer para que funcione, el codigo que muestra la notificacion es el siguiente:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder mBuilder;
    NotificationManager mNotifyMgr =(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);

    int icono = R.mipmap.ic_launcher;
    Intent i=new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

    mBuilder =new NotificationCompat.Builder(getApplicationContext(), "M_CH_ID")
            .setContentIntent(pendingIntent)
            .setSmallIcon(icono)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setVibrate(new long[] {100, 250, 100, 500})
            .setAutoCancel(true);
    mNotifyMgr.notify(1, mBuilder.build());
}

En el manifiest tengo:

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

Estoy comprobandolo en un android con version 7.0 y funciona bien, pero en un movil con android 8.0 no, Porque puede ser? tengo puesto esto en el build.gradle del app

 minSdkVersion 24
 targetSdkVersion 28
 versionCode 1
1

2 respuestas 2

0

Al final lo solucione, hay que agregar un channel. Dejandolo asi me funciona.

int icono = R.mipmap.ic_launcher;
    Intent i=new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
    NotificationChannel mChanel=new NotificationChannel("11","case",NotificationManager.IMPORTANCE_HIGH);
    mBuilder =new NotificationCompat.Builder(getApplicationContext(), "11")
            .setContentIntent(pendingIntent)
            .setSmallIcon(icono)
            .setContentTitle("Mi titulo"+remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setVibrate(new long[] {100, 250, 100, 500})
            .setAutoCancel(true);
    mNotifyMgr.createNotificationChannel(mChanel);
    mNotifyMgr.notify(1, mBuilder.build());
0

Por si alguien le ayuda esta información.

Según la documentación de firebase a partir de Android 8.0 (API 26) y versiones más recientes se recomiendan los canales de notificación, para agregarlo realizar los pasos siguientes:

1.- En el archivo AndroidManifest.xml agregar lo siguiente:

<application>
...

<meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />
<application>

2.- En el archivo strings.xml (se encuentra en res->values->strigs.xml), agregar lo siguiente:

<resources>
...

<string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>

3.- El método onMessageReceived quedaría de la siguiente manera:

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        if(remoteMessage.getNotification()!=null){
            String titulo=remoteMessage.getNotification().getTitle();
            String texto=remoteMessage.getNotification().getBody();
            generarNotificacion(titulo, texto);
        }
    }
private void generarNotificacion(String titulo, String texto) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(titulo)
                        .setContentText(texto)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
        NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

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