Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically ending an ongoing notification - Android

I am developing a GPS-based app and have just started adding in my UX features such as notifications and progress bars but I'm stuck on using an ongoing notification.

As it is a GPS app, when user tracking is started, I set up an ongoing notification to show that they are being tracked but how do I stop this notification when they tap "stop tracking" in my app? Do I have to tell the NotifyManager something? I'm basically trying to get the functionality that music players have, as in the "playing" notification appears when the user presses play, but when they pause, that ongoing "playing" notification is destroyed.

Also, I've never worked with GPS before but should I be going about this in a Service so that the user won't stop being tracked if my app is taken out of memory by the OS? Or would that not happen?

like image 923
Infiniti Fizz Avatar asked Mar 06 '11 01:03

Infiniti Fizz


People also ask

How do I turn off ongoing notifications on Android?

To find your notifications, from the top of your phone screen, swipe down. Touch and hold the notification, and then tap Settings . Choose your settings: To turn off all notifications, turn off All notifications.

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.

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 get notifications when an app is closed Android?

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.


2 Answers

I haven't done much with notifications but you might try this:

NotificationManager.cancel(id); // Where 'id' is the id of your notification

Replacing NotificationManager with the name of your instance of it, of course.

docs: http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28int%29

Or this:

Notification.Builder.setAutoCancel(true);

http://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel%28boolean%29

like image 51
John Avatar answered Oct 05 '22 05:10

John


Start

int id = 01;
NotificationCompat.Builder mBuilder =
      new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getResources().getString(R.string.service_header))
        .setContentText("Connect to: http://" + httpd.getip() + ":8080")
        .setContentIntent(pendingIntent)
        .setOngoing(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(01, mBuilder.build());

Cancel

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);
like image 40
juliusmh Avatar answered Oct 05 '22 07:10

juliusmh