Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android how can i know the current notification id to clear the notification

Now in android i put this code in one activity to show notification when a button pressed.

static int notificationCount = 0;

then

 btnNotification.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
                    PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);

                    // Construct the notification
                    Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
                    nBuilder.setContentTitle("You Have a notification!");
                    nBuilder.setContentText("See Your Notification");
                    nBuilder.setSmallIcon(android.R.drawable.btn_star);
                    nBuilder.setContentIntent(pIntent);
                   nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16

                    // Build the notification
                    Notification noti = nBuilder.build(); // required api 16

                    //Send it to manager
                        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                        manager.notify(notificationCount++,noti);
                }
            }
    );

From Notification manager, Any notification that i clicked on it, it will redirect me to another activity (NotificationActivity)

Now i put this code to clear the notification but it only clear the notification with id 0 so how can i clear the current pressed notification

public class NotificationActivitty  extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(0);
    // manager.cancelAll(); // Cancel all notifications for this app. from manager

}

I need to clear the notification by it's id if it's possible.

like image 295
Marzouk Avatar asked Jul 24 '15 16:07

Marzouk


People also ask

How do I clear my ongoing notifications on Android?

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.

How do I know if my Android notification was dismissed?

You can see your Android notification history on Android 11 through the Settings app. You first need to turn on Notification history in your phone's notification settings. You will then be able to see all the alerts you dismissed in the past 24 hours.

How do I count notifications on Android?

Right-click on drawable > new > vector asset > search for notification icon and select > finish. We are going to use this icon to show our notification count on top of it.

How do I use Notification Manager on Android?

Notification Manager. Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity. Because notifications can be very annoying, the user can disable notifications for each application.

How to clear an Android notification after a few seconds?

This example demonstrate about How to clear an Android notification after a few seconds. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How do I Turn on notifications on my Android phone?

On your Android phone or tablet, swipe down from the top of the screen (once or twice depending on your device’s manufacturer), then tap the “Gear” icon to open the “Settings” menu. Select the “Apps & Notifications” option from the menu. Next, tap “Notifications.” At the top of the screen, select “Notification History.”

How to use notification history on Android devices?

Select the “Apps & Notifications” option from the menu. Next, tap “Notifications.” At the top of the screen, select “Notification History.” Lastly, toggle the switch on at the top of the screen for “Use Notification History.” The log will be blank at first, but it will start storing notifications after you enabled the feature.

What are the different types of notifications on Android?

On some Android devices, you can choose what kind of notifications an app sends you: Alerting: You'll hear a sound, get a message on your lock screen, and find the app's icon in the status bar. Silent: Your phone won't make a sound or vibrate.


Video Answer


2 Answers

You should add a Tag to your notification and then clear you notification by providing correct id and correct tag.

You don't need notification counter if you pass the same id, because when notification sees the same id, it clears old notification and puts a new one, unless you want to show that user received multiple notifications.

private static final String TAG = "YourNotification";
private static final int NOTIFICATION_ID = 101;
private Notification notification;
public NotificationManager notificationManager;

//you can create notification with it's own id and tag, text, etc by passing 
//these variables into the method (int id, String tag, ... etc)

public void createNotification()
{
    notification = new Notification.Builder(context)
                       .setContentTitle("Content title")
                       .setContentText("Content text")
                       .setSmallIcon(R.drawable.your_small_icon)
                       .setLargeIcon(bitmapYourLargeIcon)
                       .setContentIntent(pendingIntent)
                       .addAction(R.drawable.icon, pendingIntentAction)
                       .build();
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(TAG, NOTIFICATION_ID, notification);
}

to cancel notification simply use this method:

//clears your notification in 100% cases

public void cancelNotification(int id, String tag)
{
  //you can get notificationManager like this:
  //notificationManage r= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(tag, id);
}
like image 75
Fedor Tsyganov Avatar answered Sep 21 '22 19:09

Fedor Tsyganov


you need to add this line of code when you create your notification!!!

notification.flags |= Notification.FLAG_AUTO_CANCEL;

This will cancel the notification on click.

Further reading: Open application after clicking on Notification

** Edit, adding an extra to determine if certain notification was clicked pIntent.putExtra("fromNotification", true);

like image 31
oorosco Avatar answered Sep 21 '22 19:09

oorosco