Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing notification after click

Tags:

android

I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.

I tried to work with the following code:

import android.app.NotificationManager;  public class ExpandNotification {      private int NOTIFICATION = 546;      private NotificationManager mNM;       public void onCreate() {         mNM.cancel(NOTIFICATION);         setContentView(R.layout.activity_on);         //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();     } 

I think this code executes the other class when tapped?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0); 

However the notification doesn't go away, nor does the application launch. But I'm able to swipe it to left or right to remove it but that's not what I want..

like image 452
user1756912 Avatar asked Oct 19 '12 11:10

user1756912


People also ask

How do I dismiss notifications on Android?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.

How do I remove items from my notification bar?

any options you want to remove from the notification bar. Tap on any of the switches to turn them on or off. This removes these options from the notification bar.


2 Answers

To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.

like image 117
0101100101 Avatar answered Sep 25 '22 12:09

0101100101


Use the flag Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);  // Cancel the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; 

and to launch the app:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  // Create a new intent which will be fired if you click on the notification Intent intent = new Intent(context, App.class);  // Attach the intent to a pending intent PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
like image 37
input Avatar answered Sep 24 '22 12:09

input