4

I am implementing custom push notification by integrating RemoteViews.The problem is,the buttons within the remoteview is not displaying.I am not getting what wrong I have done.

Code:

public class AlarmReceiver extends BroadcastReceiver {
    Bitmap bannerimage;
    private static int MY_NOTIFICATION_ID=1;
     NotificationManager notificationManager;
     Notification myNotification;

    @SuppressLint("NewApi")
    @Override
    public void onReceive(Context context, Intent intent) {


        bannerimage = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.dummyturkey);
        MY_NOTIFICATION_ID=1;
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.custom_push_layout);
        Intent myIntent = new Intent(context,DoSomething.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 
                MY_NOTIFICATION_ID, 
                myIntent, 
                Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
        System.out.println("Alarm fired AlarmReciever:"+mydate);

        remoteViews.setImageViewBitmap(R.id.imgbanner,bannerimage);

        Notification myNotification  = new Notification.Builder(context)
        .setContent(remoteViews)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setWhen(System.currentTimeMillis())
        .setSound(alarmSound)
        .setAutoCancel(false).build();



        NotificationManager notificationManager = 
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);


        }

    }

The XML File custom_push_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgbanner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This chicken has send you a friend request" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnaccept"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Accept" />

        <Button

            android:id="@+id/btncancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnaccept"

            android:text="Cancel" />
    </RelativeLayout>

</LinearLayout>

The notification is coming,but it is only displaying the imageview and not the buttons or the textview within the layout.

Please give the solution with reference to the above code only,i.e. what wrong I am doing or what I am missing.Please dont post a fresh code.

kgandroid
  • 5,507
  • 5
  • 39
  • 69

1 Answers1

4

With Notification.Builder(context).setContent you are setting the normal view layout for the notification. According to Notifications API Guide the normal view layout height is limited to 64dp.

The height available for a custom notification layout depends on the notification view. Normal view layouts are limited to 64 dp, and expanded view layouts are limited to 256 dp.

What you need to do, is to set contentView and bigContentView to the Notification object. Create two separate layouts, one for the normal and one for the big view layout, and create two RemoteViews.

 RemoteViews customViewSmall = new RemoteViews(context.getPackageName(), R.layout.custom_notification_small);
 RemoteViews customViewBig = new RemoteViews(context.getPackageName(), R.layout.custom_notification_big);

... 
set the values of the views
...

 Notification myNotification  = new Notification.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setWhen(System.currentTimeMillis())
        .setSound(alarmSound)
        .setAutoCancel(false).build();

 myNotification.contentView = customViewSmall;
 myNotification.bigContentView = customViewBig; 

Please note that the bigContentView is available from API16. As well in the UI xml, add color for all TextView views.

EDIT: In the v4 support library 24, NotificationBuilderCompat has new method setCustomBigContentView(), so instead setting the remoteViews to Notification object, just use the NotificationBuilderCompat. You can see it here: Notification API Guide.

Result:

Result:

nikolaDev
  • 1,802
  • 2
  • 14
  • 15
  • Hi, this seems to be totally ignored on Google Pixel Android 10, which, would show a default notification when assigning the remoteView to the small and big content view. Would you know why? – Sinapse May 27 '20 at 05:47