Firebase Expandable Notification Mostrar imagen cuando la aplicación está en segundo plano

Estoy implementando las notificaciones de FCM en Android, pero ¿cómo difieren las notificaciones en función del estado de la aplicación (de fondo y de primer plano)?

Ver las notificaciones de la imagen

Estoy enviando la notificación usando FCM API con Postman y esta es la estructura de notificación:

{ "notification": { "title": "Notification title", "body": "Notification message", "sound": "default", "color": "#53c4bc", "click_action": "MY_BOOK", "icon": "ic_launcher" }, "data": { "main_picture": "URL_OF_THE_IMAGE" }, "to" : "USER_FCM_TOKEN" } 

La imagen a render se toma de data.main_picture .

He implementado mi propio FirebaseMessagingService que hace que las notificaciones se muestren perfectamente en estado de primer plano. El código de notificación es el siguiente:

 NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle(); notiStyle.setSummaryText(messageBody); notiStyle.bigPicture(picture); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(bigIcon) .setContentTitle(title) .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setStyle(notiStyle); code here NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); 

Sin embargo, en segundo plano, el servicio ni siquiera se ejecuta. En el AndroidManifest.xml , los servicios de Firebase se declaran de la siguiente manera:

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

Mi problema no es el LargeIcon o SmallIcon pero que muestra la imagen grande.

Gracias por su apoyo.

Los notification messages FCM no admiten el archivo largeIcon o bigPicture.

Si usted los necesita mientras que en fondo usted puede utilizar un data message FCM.

Para los mensajes de datos siempre se llama al método onMessageReceived(message) , por lo que puede utilizar el método message.getData() y crear su notificación personalizada.

Más información sobre mensajes de notificación y mensajes de datos aquí: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

Vea mi FirebaseMessagingService

 public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebaseMessageService"; Bitmap bitmap; /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ @Override public void onMessageReceived(RemoteMessage remoteMessage) { // There are two types of messages data messages and notification messages. Data messages are handled // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app // is in the foreground. When the app is in the background an automatically generated notification is displayed. // When the user taps on the notification they are returned to the app. Messages containing both notification // and data payloads are treated as notification messages. The Firebase console always sends notification // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options // Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values. //You can change as per the requirement. //message will contain the Push Message String message = remoteMessage.getData().get("message"); //imageUri will contain URL of the image to be displayed with Notification String imageUri = remoteMessage.getData().get("image"); //If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened. //If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened. String TrueOrFlase = remoteMessage.getData().get("AnotherActivity"); //To get a Bitmap image from the URL received bitmap = getBitmapfromUrl(imageUri); sendNotification(message, bitmap, TrueOrFlase); } /** * Create and show a simple notification containing the received FCM message. */ private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("AnotherActivity", TrueOrFalse); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setLargeIcon(image)/*Notification icon image*/ .setSmallIcon(R.drawable.firebase_icon) .setContentTitle(messageBody) .setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(image))/*Notification with Image*/ .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } /* *To get a Bitmap image from the URL received * */ public Bitmap getBitmapfromUrl(String imageUrl) { try { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(input); return bitmap; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } } 

REFERENCIA AQUI

Si su problema está relacionado con mostrar Big Image, es decir, si está enviando una notificación push con una imagen de la consola firebase y muestra la imagen sólo si la aplicación está en primer plano. La solución para este problema es enviar un mensaje push con sólo campo de datos.

 { "data": { "image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg", "message": "Firebase Push Message Using API" "AnotherActivity": "True" }, "to" : "device id Or Device token" } 

Esto definitivamente resuelve el problema.

Si su espera sólo una notificación en una bandeja del sistema para su aplicación, a continuación, la siguiente solución puede resolver el problema, hasta FCM viene con la solución adecuada.

  1. Quitar MyFirebaseMessagingService de manifiesto.

     <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> 
  2. MyGcmReceiver Extiende la clase GcmReceiver y endereza la lógica de notificación.
  3. Añadir MyGcmReceiver en el manifiesto

      <receiver android:name=".MyGcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="package_name" /> </intent-filter> </receiver> 
  4. Cancelar todas las notificaciones antes de notificar la notificación. (De lo contrario firebase también, muestra la notificación cuando la aplicación está en segundo plano)

Puede enviar mensajes con esta herramienta de cliente de descanso. Uso de esta herramienta También puede enviar mensajes a la aplicación de cliente en segundo plano y en primer plano. Para enviar un mensaje utilizando la API, puede utilizar una herramienta llamada AdvancedREST Client, es una extensión de cromo y enviar un mensaje con los siguientes parámetros.

Herramienta cliente de descanso Enlace: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Use esta url: – https://fcm.googleapis.com/fcm/send Tipo de contenido: application / json Autorización: key = Su clave de servidor De o Authoization clave (ver abajo ref)

{"Data": {"image": " https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg ", "mensaje": "Mensaje de Push de Firebase con API" "AnotherActivity" : "True"}, "to": "identificador de dispositivo o símbolo de dispositivo"}

La clave de autorización se puede obtener visitando la consola de desarrolladores de Google y haga clic en el botón Credenciales en el menú de la izquierda para su proyecto. Entre las claves API enumeradas, la clave del servidor será su clave de autorización.

Y necesita poner tokenID del receptor en la sección "a" de su solicitud POST enviada usando API.

Y este pedazo de android código // mensaje contendrá el mensaje de empuje

  String message = remoteMessage.getData().get("message1"); //imageUri will contain URL of the image to be displayed with Notification String imageUri = remoteMessage.getData().get("image"); //If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened. //If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity2 will be opened. String TrueOrFlase = remoteMessage.getData().get("AnotherActivity"); //To get a Bitmap image from the URL received bitmap = getBitmapfromUrl(imageUri); sendNotification(message, bitmap, TrueOrFlase); 

Enviar notificación de imagen grande desde la consola de Firebase: funciona para aplicaciones de fondo y de primer plano

En lugar de onMessageReceived, anule zzm () de FirebaseMessagingService y cree su notificación personalizada desde aquí

 @Override public void zzm(Intent intent) { Log.e(TAG, "zzm : " + intent); createBigPictureNotification(); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { } 
  • Cómo verificar el ID del remitente de GCM de deviceToken en Parse.com
  • Cómo anular onPushReceive () de ParsePushBroadcastReceiver?
  • Icono de notificación con el nuevo sistema de mensajería de Cloud Firebase
  • No recibe mensajes de GCM en Android después de que la aplicación se aleje de tareas recientes
  • Cómo almacenar únicamente un ID de registro de GCM en MySQL
  • Android GCM un dispositivo tiene múltiples registro
  • Pushes no recibidos después de que la aplicación esté cerrada
  • GCM devuelve un tipo de mensaje nulo
  • ¿Está lista la producción de GCM? ¿Deberíamos empezar a usarlo ahora?
  • Clave de aplicación de desarrollo y secreto de aplicación para Android
  • Versión mínima de servicios de Google Play para la notificación de emisión
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.