Answer the question
In order to leave comments, you need to log in
Why doesn't setOnClickPendingIntent fire in Android 7.0 Nougat in a notification?
This code (see below) works fine in API below and above 24. Doesn't work only in Android 7.0 Nougat. Question: why and how to fix it?
Create a custom notification:
layout.control_notification.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/notify_button"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.control_notification);
Intent actionIntent = new Intent(ACTION_NOTIFICATION_BUTTONS);
remoteViews.setOnClickPendingIntent(R.id.notify_button,
PendingIntent.getBroadcast(
this,
1,
actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT
));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CONTROL_CHANNEL_ID)
.setContent(remoteViews)
.setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_HIGH);
android:focusable="true"
android:clickable="true"
Answer the question
In order to leave comments, you need to log in
Managed to solve the problem myself.
The bug is clearly visible on the emulator, where you can make any number of clicks on the same point. The essence of the bug is that clicks in the custom notification area are periodically lost. There are such points where you can stick to infinity until you move the mouse. All this is random.
It looks like this bug is related to internal optimization, redundant. If the root layout is empty, then the click handler will not be hung on it at all via setOnClickPendingIntent. If not empty, then clicks are random.
To remove the bug, it is enough to add an element of type Button/ImageButton to the markup. Just add it without hanging a click handler on it, and then clicks on all other elements on which setOnClickPendingIntent is hung will work normally.
An example of a broken markup where the only fix is to uncomment the ImageButton.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:layout_height="64dp">
<FrameLayout
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_notify_1"/>
</FrameLayout>
<FrameLayout
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_notify_2"/>
<!--<ImageButton
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="center"
android:layout_margin="0dp"
android:padding="0dp"
android:src="@drawable/ic_notify_2"/>-->
</FrameLayout>
</LinearLayout>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question