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());
First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.
The key differentiator is that getting push notifications are the default for Android apps. For iOS, the user is prompted to allow notifications when they first open a new app, so the default is not getting push notifications for any given app.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With