25

I have an app that has an ongoing notification to help with memorization. I want to be able to dismiss this notification with one of the action button, but I don't want to open the app when the button is hit. I would prefer to use the built-in notification action buttons and not create a RemoteViews object to populate the notification. I saw one post mention using a BroadcastReceiver on this button which is received in my app, and though his tutorial was quite unhelpful, it sounds like this is headed in the right direction.

Intent resultIntent = new Intent(getBaseContext(), Dashboard.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
    Intent cancel = new Intent(getBaseContext(), CancelNotification.class);
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(cancel);
        PendingIntent pendingCancel =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );

    NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
        mb.setSmallIcon(R.drawable.cross_icon);
        mb.setContentTitle(ref);
        mb.setContentText(ver);
        mb.setPriority(NotificationCompat.PRIORITY_LOW);
        mb.setOngoing(true);
        mb.setStyle(new NotificationCompat.BigTextStyle().bigText(ver));
        mb.setContentIntent(resultPendingIntent);
        mb.addAction(R.drawable.ic_cancel_dark, "Dismiss", pendingCancel);

    manager.notify(1, mb.build());  
2
  • Can you be more clear about you want? Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. Or, as you said, use a BroadcastReceiver bind to a action button to do that. The BroadcastReceiver only need call cancel() on the notification id passed to it. You also need cancel/finish the event that raise the notification
    – ramaral
    Commented Nov 2, 2013 at 17:06
  • 1
    I guess just more in general, I just want to be able to push a notification button to do something without opening the app. So how exactly would I implement the BroadcastReceiver to perform any action, the tutorial I tried to read was very unclear.
    – cjbrooks12
    Commented Nov 2, 2013 at 17:34

3 Answers 3

82

Start with this:

int final NOTIFICATION_ID = 1;

//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());  

Create the BroadcastReceiver:

public class ButtonReceiver extends BroadcastReceiver {

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

        int notificationId = intent.getIntExtra("notificationId", 0);

        // Do what you want were.
        ..............
        ..............

        // if you want cancel notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
    }
}  

If you don´t want show any activity when user click on notification, define the intent passed in setContentIntent in this way:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent); 

To close notification tray when clicked, call setAutoCancel() with true when building the notification: mb.setAutoCancel(true);

5
  • 3
    thanks for the code, it does what it says but i am facing one additional issue. The code dismisses the notification but the notification tray remains open. How do i hide/close it pragmatically?
    – mudit
    Commented Jun 14, 2015 at 16:15
  • @mudit Set setAutoCancel to true when building the notification: mb.setAutoCancel(true);
    – ramaral
    Commented Jun 15, 2015 at 19:27
  • 1
    onReceive is never called in my case
    – Roel
    Commented Oct 12, 2015 at 15:30
  • 9
    I followed exactly your example but I forgot to register the broadcastreceiver in the manifest: It works now!
    – Roel
    Commented Oct 13, 2015 at 7:59
  • great answer...
    – Ramesh
    Commented Nov 30, 2021 at 15:04
5

The accepted solution is not working in Android 8.1 and onwards.

Follow the same steps as in the accepted answer, but update this line:

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);

See also PendingIntent.FLAG_UPDATE_CURRENT

1
  • I think this is only needed if using the same receiver to dismiss as the one used to display the notification in the first place, because the PendingIntent would already exist in that case? Anyway it works for me.
    – Nicolas
    Commented Jan 17, 2021 at 14:28
0

I have tried using answer from @ramaral , but it is not working as the notificationId at the onReceive method always -1. I have found the solution for this problem by configuring

from

PendingIntent.getBroadcast(context, 0, approvedIntent, 0)

to

PendingIntent.getBroadcast(
    context,
    notificationId,
    approvedIntent,
    PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)

Here is an example that you can follow.

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    val notificationId = Random.nextInt()
    
    //Making Approve Intent for Button Action
    val approvedIntent = Intent(context, 
        BroadcastReceiverOuting::class.java).apply {
            action = ActionOuting.ACTION_APPROVED.name
            putExtra(
                Constant.NOTIFICATION_ID,
                notificationUid
            ) //Compulsory to dismiss this notification
            putExtra(Constant.STUDENT_IC, studentIc)
            putExtra(Constant.OUTING_ID, outingId)
        }
    val approvedPendingIntent: PendingIntent =
        PendingIntent.getBroadcast(
            context,
            notificationUid,
            approvedIntent,
            PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
        )
    val notification: Notification =
        notificationCompatBuilder
            .setStyle(bigTextStyle)
            .setContentTitle(title)
            .setContentText(description)
            .setSmallIcon(R.drawable.ic_logo_128)
            .setContentIntent(notifyPendingIntent)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setAutoCancel(true)
            .addAction(
                R.drawable.ic_baseline_approve_check_24,
                context.getString(R.string.approve),
                approvedPendingIntent
            )
           .build()
    NotificationManagerCompat.from(context.applicationContext).apply {
       notify(notificationUid, notification)
    }
}

And then at the BroadcastReceiverOuting

override fun onReceive(context: Context?, intent: Intent?) {
    if (intent != null && intent.action != null) {
        val notificationUid = intent.getIntExtra(Constant.NOTIFICATION_ID, -1)
        val studentIC = intent.getStringExtra(Constant.STUDENT_IC)!!
        val outingID = intent.getStringExtra(Constant.OUTING_ID)!!

        //Do something...

        //To dismiss the notification
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(notificationUid)
    }
}

Lastly, don't forget the AndroidManifest.xml .

<receiver
    android:name=".utils.BroadcastReceiverOuting"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.