0

I make a notification and what i want to do is, when the user clicks the notification I sent to clear it, i want a pending broadcast to be executed. there is a method called 'setDeleteIntent'. According to the documentation of this method, it should starts a pending BraodCast Receiver when the user clear the notification received.7

I create a Broadcast Receiver class as shown below in the code, and I supply the the 'setDeleteIntent' method with pending broadcast. But at run time, when the notification received then cleared, the Log message in 'onReceive' of the BroadcastReceiver did not show on the console!!

please have a look at the code below, and please let em k know why the 'onReceive' of the broadcastReceiver never executes.

note: the BroadCasrReceiver was registered using 'LocalBroadCastReceiver' as shown in the code,.

code:

    String title = getString(R.string.app_name);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setContentTitle(title);
    builder.setContentIntent(contentIntent).setAutoCancel(true);

    //builder.setWhen(Long.valueOf(eu.man.m24wsapp.utils.gcm.TimeUtils.getNowTS()) + 5000);
    builder.setLights(Color.argb(1, 0, 0, 255), 1000, 400);
    builder.setOngoing(false);
    builder.setAutoCancel(true);


    builder.setDeleteIntent(getPendingBroadCast());
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, builder.build());
    ....
    ....
    ....

    //getPendingBroadCast method
    private PendingIntent getPendingBroadCast() {
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new BCRDismissNotification(),
            new IntentFilter(BCRDismissNotification.ACTION_DISMISSED_NOTIFICATION));

    Intent intent = new Intent(this, BCRDismissNotification.class);
    //intent.setAction(BCRDismissNotification.ACTION_DISMISSED_NOTIFICATION);
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

BroadCast Receiver:

public class BCRDismissNotification extends BroadcastReceiver {
private final static String TAG = BCRDismissNotification.class.getSimpleName();
public final static String ACTION_DISMISSED_NOTIFICATION = "action_dismissed_notification";

@Override
public void onReceive(Context context, Intent intent) {

    Log.i(TAG, "*********************   BCR     **************");
}

}

Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

1

You have pretty much answered your own question. This can't work with LocalBroadcastManager!

When you register a receiver with using LocalBroadcastManager, it only listens for broadcast Intents that are broadcast by your application. In your case, the broadcast Intent is sent by Android's notification framework, which is not part of your application. In this case, your registered receiver will not get triggered.

You need to register your receiver for global broadcasts. You can do this either by creating an instance of your receiver and calling registerReceiver() on the application Context OR you can just add your receiver to the manifest with a <receiver> tag and make sure that you set android:exported="true"

David Wasser
  • 93,459
  • 16
  • 209
  • 274