androidpush-notificationnotificationscustom-notification

Implement android expandable notification with xml content?


I'm trying to implement custom notification with an action buttons

But when I try to implement this, the actions buttons and the app name doesn't appear.

My implementation:

    RemoteViews remoteViews = new 
    RemoteViews(getPackageName(),R.layout.emotions_notification);
    Intent receiver = new Intent();
    receiver.setAction("Action");
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, receiver, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "feelBetter.emotions")

      .setContent(remoteViews)
      .setContentIntent(pendingIntent)
      .setSmallIcon(R.drawable.ic_status_bar)

      .setPriority(NotificationCompat.PRIORITY_DEFAULT)
      .setColor(Color.parseColor("#322176"))
      .setLights(Color.parseColor("#322176"), 1000, 1000)
      .addAction(R.drawable.common_google_signin_btn_icon_dark_normal, "Action", pendingIntentYes)
      .setSound(uri)
      .setAutoCancel(true);

    notificationManager = NotificationManagerCompat.from(this);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(id, mBuilder.build());

How to implement this?


Solution

  • try this

    // Get the layouts to use in the custom notification
    RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
    RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
    
    // Apply the layouts to the notification
    Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .build();