Search code examples
androidnotificationsremoteview

Create custom notification with custom view


I want to create a notification that handle incoming call like this

enter image description here

I tried to use RemoteViews but not success. Maybe I cannot interact with it. This is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            style="Custom Notification Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:text="aasdasdasd" />

        <TextView
            android:id="@+id/text"
            style="Custom Notification Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_toRightOf="@id/image"
            android:text="aasdasdasd" />

    </LinearLayout>


    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

This is my show notification function:

 private void customNotification() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);


        Intent intent = new Intent(this, CallingActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        remoteViews.setImageViewResource(R.id.image, R.drawable.bank);
        remoteViews.setTextViewText(R.id.title, "Custom notification");
        remoteViews.setImageViewResource(R.id.icon, R.drawable.individual);
        remoteViews.setTextColor(R.id.title, ResourceUtil.getColorId(R.color.black));
        remoteViews.setTextViewText(R.id.text, "This is a custom layout");
        remoteViews.setTextColor(R.id.text, ResourceUtil.getColorId(R.color.black));
        AppWidgetManager.getInstance(this).updateAppWidget(getComponentName(), remoteViews);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSound(defaultSoundUri)
                .setOngoing(true)
                .setContentTitle("Calling")
                .setContentText("Message")
                .setSmallIcon(R.drawable.login_logo)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setContent(remoteViews);


        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


        mNotificationManager.notify(1, builder.build());
    }

Can anyone tell me about this?


Solution

  • That doesn't require a custom notification at all - that is a standard notification using standard notification actions.

    The behavior you're seeing is a heads up notification, which is only triggered if you use setFullScreenIntent() (which is also how it stays visible for a long period of time) and/or is high priority and uses ringtones or vibrations.