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)?
- Notificaciones push en Android 5 no se entregan cuando la aplicación en segundo plano (Titanium) - Pero lo hace en primer plano
- GET_ACCOUNTS durante el uso de GCM - ¿Por qué es necesario?
- Cómo hacer la notificación push desde el servidor al móvil Android
- Notificaciones push de Android con XMPP
- Cómo agregar una notificación de inserción en mi propia aplicación de Android
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.
- FCM (Firebase Cloud Messaging) cómo enviar a todos los teléfonos?
- No puede conectarse al servidor de Google Cloud Connection
- Error interno: la clase de receptor de reintento no se ha configurado todavía
- El icono de notificación push de Android 5.1 está en blanco
- Notificación push de Android: determina si se muestra Notification.bigContentView
- ¿Cómo aplicar la acción de toque de notificación a una actividad específica en android?
- Se ha producido un error en el registro de ServiceWorker: DOMException: Sólo se permiten orígenes seguros (consulte: https://goo.gl/Y0ZkNV)
- Enviar notificaciones push a Android
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.
-
Quitar MyFirebaseMessagingService de manifiesto.
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
- MyGcmReceiver Extiende la clase GcmReceiver y endereza la lógica de notificación.
-
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>
-
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) { }
- Conexión a un dispositivo Android USB en un contenedor Docker vía adb
- MPAndroidChart ajuste gráfico padding / offset