Help deal with notifications. The problem is that my clicks on the buttons in the notification are not processed. Where am I doing wrong? Clicking on the notification itself is processed if you uncomment the build '.setContentIntent (stopPendingIntent)'. Buttons also work if you add via '.addAction (R.mipmap.stop_black_18dp, "Stop", stopRendingIntent)' But you need to through your layout. I'm testing on Motorola g4 Android 7.1.1 I would be grateful for any help. notification method
private void prepareNotification() { Intent stopIntent = new Intent(NoiseActivity.ACTION_STOP); stopIntent.putExtra(ACTION_STOP, 1); stopIntent.setAction(NoiseActivity.ACTION_STOP); Intent pauseIntent = new Intent(NoiseActivity.ACTION_PAUSE); pauseIntent.putExtra(ACTION_PAUSE, 2); pauseIntent.setAction(NoiseActivity.ACTION_PAUSE); Intent timerIntent = new Intent(NoiseActivity.ACTION_TIMER); timerIntent.setAction(NoiseActivity.ACTION_TIMER); timerIntent.putExtra(ACTION_TIMER, 3); PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 100, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 101, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent timerPendingIntent = PendingIntent.getBroadcast(this, 102, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notlayout); notificationLayout.setOnClickPendingIntent(R.mipmap.stop_black_18dp, stopPendingIntent); notificationLayout.setOnClickPendingIntent(R.mipmap.pause, pausePendingIntent); notificationLayout.setOnClickPendingIntent(R.mipmap.timer, timerPendingIntent); Notification build = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_image1) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContent(notificationLayout) // .setContentIntent(stopPendingIntent) // .addAction(R.mipmap.stop_black_18dp, //"Stop",stopRendingIntent) // .setAutoCancel(true) .build(); build.contentIntent=stopPendingIntent; build.contentIntent=pausePendingIntent; build.contentIntent=timerPendingIntent; // build.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, build); } the receiver is registered in the manifest
<receiver android:name=".core.NoiseActivity$NoiseBroadcastReceiver"> <intent-filter> <action android:name="ua.i.pl.backbroadcast.stop"/> <action android:name="ua.i.pl.broadcast.pause"/> <action android:name="ua.i.pl.broadcast.timeropen"/> </intent-filter> </receiver> in the receiver itself will switch on action
public class NoiseBroadcastReceiver extends BroadcastReceiver { public NoiseBroadcastReceiver() { } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); } } markup for RemoteView
<?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="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/pause_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:src="@mipmap/pause"/> <ImageView android:id="@+id/stop_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:src="@mipmap/stop_black_18dp"/> <ImageView android:id="@+id/timer_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:src="@mipmap/timer"/> </LinearLayout>