0

I want to have the custom UI for the push notifications. For that I am using RemoteViews. code for creating notifications :

 RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
    RemoteViews expandedNotificationView = new RemoteViews(context.getPackageName(), R.layout.expanded_custom_notification);
    notificationView.setTextViewText(R.id.notification_title, messagesToBeShown);
    expandedNotificationView.setTextViewText(R.id.expanded_notification_title, "Message Received in expanded ");
    expandedNotificationView.setTextViewText(R.id.expanded_notification_message, messagesToBeShown);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setContentIntent(pendingIntent);
    builder.setPriority(Notification.PRIORITY_MAX);
    Notification notification = builder.build();
    notification.contentView = notificationView;
   //        Notification notification = new             NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
   //                .setContentIntent(pendingIntent).setContent(notificationView)
   //                .setAutoCancel(true).build();
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        notification.bigContentView = expandedNotificationView;
    }
    notification.flags |= Notification.DEFAULT_LIGHTS |      Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(R.string.notification_number, notification);

XMl file for custom layout:

expanded_custom_notification.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:orientation="horizontal">

<ImageView
    android:id="@+id/expanded_notifiation_image"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<LinearLayout
    android:layout_width="80dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/expanded_notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />

    <TextView
        android:id="@+id/expanded_notification_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />
</LinearLayout>

custom_notification.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:orientation="horizontal"
android:weightSum="100">

<ImageView
    android:id="@+id/notifiation_image"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/notification_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#fff" />

<Button
    android:id="@+id/mark_task_complete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:text="mark as complete" />

</LinearLayout>

The problem with this code is :

  • not expanding on swipe
  • Behaviour is not defined sometimes it shows bigContentView, sometimes ContentView

Please help me. Thanks in advance.

yAnTar
  • 4,269
  • 9
  • 47
  • 73
Awadesh
  • 3,530
  • 2
  • 20
  • 32

1 Answers1

0

Try adding these lines before your builder.build()

builder.setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
fjibril
  • 86
  • 1
  • 3