Search code examples
androidnotificationsbroadcastreceivernotification-action

BroadcastReceiver does not get data from notification button


I am trying to make notification with remove button, notifications are stored in SQLite DB and after the press, I want to remove "record" by id. Firstly I get a notification, store it to db , store unique id and pass it to a method which creates a notification. It gets fine in the method.

    public static String CUSTOM_ACTION = "com.example.app.Services.REMOVE";
    Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
    snoozeIntent.setAction(CUSTOM_ACTION);
    snoozeIntent.putExtra("NOT_WORKING", String.valueOf(id));
    PendingIntent snoozePendingIntent =
            PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_stat_bell)
            .setContentTitle("Loggly")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .addAction(R.drawable.ic_delete_forever_black_24dp, "Remove", snoozePendingIntent);

then I have a broadcast receiver for handling data from notification. Here is how it looks in my manifest file.

    <receiver android:name=".MyBroadcastReceiver"  android:exported="false">
        <intent-filter>
            <action android:name="com.example.app.Services.REMOVE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

And here is the broadcast receiver the value which I got from extras is always null ... I have been trying to find a solution, but with no result.

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String id;

        if (extras != null) {
            id = extras.getString("NOT_WORKING");
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
            if (notificationManager != null) {
                notificationManager.cancel(0);
            }

            Intent inten = new Intent(context, RemoveService.class);
            inten.putExtra("NOT_WORKING", id);
            context.startService(inten);
        }
    }
}

Finally, I am starting intent service, which should delete the "record" from the database, but when the broadcast receiver does not receive id it can't do anything.


Solution

  • I just created a sample notification on my side and the problem is the way you are creating the pendingIntent. Just add the proper flag to the pendingIntent parameter and it will work fine.

    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    You can use FLAG_ONE_SHOT as well. If it satisfies your use case. You can go through different flags which can be used in PendingIntent