0

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.

2 Answers2

2

You are setting the setOnClickPendingIntent of the RemoteViews after supplying it as the custom RemoteView of the Notification.

Try setting setOnClickPendingIntent before calling setContent. E.g.:

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]) );

Intent closeButton = new Intent(this,StopReceiver.class);
closeButton.setAction(StopReceiver);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0);
 contentView.setOnClickPendingIntent(R.id.stop,pendingIntent);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContent(contentView);
Filipe Batista
  • 1,862
  • 2
  • 24
  • 39
0

If you are using listview in widget then it is very costly to set PendingIntents on the individual items, and is hence not permitted. Instead a single PendingIntent template can be set on the collection, you can use setOnClickFillInIntent()

  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_article_list_item);
  Intent intent = new Intent(context, ArticleDetailActivity.class);  //activity to start
  intent.putExtra("article",articleList.get(position));//any data to send
   //don't use pending intent here
  views.setOnClickFillInIntent(R.id.widget_item, intent);

You can use this in overridden method getViewAt(final int position) { ... }

Sanjay Sharma
  • 3,687
  • 2
  • 22
  • 38