I'm using Firebase's notifications service to receive push notifications in my APP, the problem is when the application is in the background or dead, if the notifications arrive but they do not look like I built them (I assign a small icon, large icon, sound and vibration), I was already checking in the documentation of firebase come some goals that have to be added in the Manifest (the annexes below) to assemble the notification if you do not have data, the problem is that only the background and the icon are set, I would like to know if there is a method that creates the notification with certain parameters so I can establish mine when the app is in the background or dead.
meta-data in the MANIFEST
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/icono" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimary" />
JAVA ARCHIVE FOR THE TOKEN
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService{
public static final String TAG = "LePanier";
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG,"Token: " + token);
}
}
JAVA ARCHIVE THAT RECEIVES THE MESSAGE
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "PRUEBA";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String From = remoteMessage.getFrom(); //De donde viene el mensaje
Log.d(TAG, "Mensaje Recibido de: " + From);
if (remoteMessage.getNotification()!= null)
{
Log.d(TAG,"Notificación: " + remoteMessage.getNotification().getBody());
String Titulo = remoteMessage.getNotification().getTitle();
String Mensaje = remoteMessage.getNotification().getBody();
MostrarNotificacion(Titulo,Mensaje);
}
if (remoteMessage.getData().size() > 0)
{
Log.d(TAG,"Data: " + remoteMessage.getData());
String Titulo = remoteMessage.getNotification().getTitle();
String Mensaje = remoteMessage.getNotification().getBody();
MostrarNotificacion(Titulo,Mensaje);
}
}
FUNCTION TO CREATE MY NOTIFICATION
private void MostrarNotificacion(String Titutlo, String Mensaje)
{
Uri sonido = Uri.parse("android.resource://com.lepanier.lepanier/" + R.raw.timbrebicicleta);
long[] vibrate = {0,100,200,300};
Resources resources = getResources();
int color1 = resources.getColor(R.color.colorPrimary);
Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.icono_large);
Intent intent = new Intent(this, PaginaInicio.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.pushnotifications);
notificationBuilder.setContentTitle(Titutlo);
notificationBuilder.setContentText(Mensaje);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(sonido);
notificationBuilder.setVibrate(vibrate);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setColor(color1);
notificationBuilder.setLargeIcon(icon);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}