0

I have notification with custom view that contains button. When user press the button in notification it should get some Toast message. However Toast never shows up because onReceive is never called. What I'm doing wrong?

 int icon = R.drawable.play; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, "Custom Notification", when); RemoteViews notificationView = new RemoteViews(this.getPackageName(),R.layout.notification); PendingIntent listenerIntent = PendingIntent.getService(this, 0, new Intent(this, AudioControlListener.class), 0); notificationView.setOnClickPendingIntent(R.id.background, listenerIntent); notification.contentView = notificationView; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent = contentIntent; startForeground(1, notification); 

my broadcast receiver class

 public static class AudioControlListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Button pressed", Toast.LENGTH_SHORT).show(); } } 
2
  • You want to show the toast in the phone's home page? Commented Oct 12, 2016 at 10:09
  • That doesn't care. I just want that trigger onReceive method. Commented Oct 12, 2016 at 10:10

1 Answer 1

2

You're using PendingIntent.getService() with an Intent that has a BroadcastReceiver target. You need to use PendingIntent.getBroadcast() instead. And make sure your Receiver is registered in the manifest.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.