i see many post on stackoverflow but i can't find a fix to my problem.
I have a notification with a custom content view with a button linked with a RemoteViews. I followed this link (Adding button action in custom notification) for attach an action to my button, but my BroadcastReceiver is never fired. The code:
private void createNotification(int index){
final int id = index;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setInt(R.id.container,"setBackgroundColor",ContextCompat.getColor(this,BG_COLORS[index]));
contentView.setImageViewResource(R.id.logo,BUTTON_OFF[index]);
contentView.setTextViewText(R.id.sound, getString(SOUND_NAME[index]) );
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContent(contentView);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, id, notificationIntent, 0);
Notification notification = mBuilder.build();
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent closeButton = new Intent(this,StopReceiver.class);
closeButton.setAction(StopReceiver);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0);
contentView.setOnClickPendingIntent(R.id.stop,pendingIntent);
mNotificationManager.notify(id, notification);
}
public static class StopReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Never called
Log.d(LOGTAG,"Received Cancelled Event");
...
}
}
I have also declared my receiver in my AndroidManifest.xml:
<receiver android:name="StopReceiver" android:enabled="true" />
Thank you.