Android show notification con un popup encima de cualquier aplicación
Con el siguiente código, mi notificación solo se agrega a la barra de notificación, no se muestra ningún mensaje de estilo emergente como si recibiera un mensaje de whatsapp cuando está en otra aplicación. ¿Qué hace que eso suceda a una notificación?
private void sendNotification(int distance, ViewObject viewObject) { Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); notificationIntent.putExtra("path", viewObject.getPath()); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(notificationIntent); PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle())); bigText.setBigContentTitle(getString(R.string.hello)); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_wald_poi) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi)) .setColor(getResources().getColor(R.color.primary)) .setContentTitle(getString(R.string.hello)) .setContentIntent(notificationPendingIntent) .setContentText(String.format(getString(R.string.notification), viewObject.getTitle())) .setDefaults(Notification.DEFAULT_ALL) .setStyle(bigText); builder.setAutoCancel(true); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, builder.build()); }
Si desea utilizar Heads-up Notifications
como este:
Establezca la prioridad de notificación en Notification.PRIORITY_HIGH
o Notification.PRIORITY_MAX
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_wald_poi) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi)) .setColor(getResources().getColor(R.color.primary)) .setContentTitle(getString(R.string.hello)) .setContentIntent(notificationPendingIntent) .setContentText(String.format(getString(R.string.notification), viewObject.getTitle())) .setDefaults(Notification.DEFAULT_ALL) .setStyle(bigText) .setPriority(Notification.PRIORITY_HIGH);
Aquí hay más información 🙂
Debe establecer la prioridad de notificación en Notification.PRIORITY_HIGH
o Notification.PRIORITY_MAX
. Tuve que hacer también .setDefaults(Notification.DEFAULT_ALL)
.