Android: How to Create an "Ongoing" Notification

create an ongoing notification android

public void showNotification() {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager nMN = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(this)
.setContentTitle("Notification Title")
.setContentText("Notification content")
.setSmallIcon() //set your icon
.setVibrate(new long[] { 1000, 1000 })
.setLights(Color.BLUE, 700, 500)
.setSound(alarmSound)
.setOngoing(true)
//.setStyle(new NotificationCompat.BigTextStyle(NorProv))
.build();
nMN.notify(NOTIFICATION_ID, n);
}

UPDATE

Assign Notification.FLAG_ONGOING_EVENT flag to your Notification like so :

notif.flags = Notification.FLAG_ONGOING_EVENT;

UPDATE #2

This is used to set priorities to your notification:

.setPriority(Notification.PRIORITY_LOW);
//other priorities: MIN, HIGH, DEFAULT, MAX

To hide the icon just try to remove the .setSmallIcon() and only use a .setLargeIcon()

Android: How to create an Ongoing notification?

Assign Notification.FLAG_ONGOING_EVENT flag to your Notification.

Sample code:

yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...

If you aren't familiar with the Notification API, read Creating Status Bar Notifications on Android developers website.

Android - How to create a Permanent Notification

On your notification builder use .setOngoing(true). This will prevent the user from removing your notification.

See the Notification Builder Documentation for more info:
http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing%28boolean%29

Android notification rebuilds instead of updating?

In the end i solved it by using custom download intent service class which extends IntentService and when i'm updating the notification i'm controlling if the last % is different than the current one (since the updateProgress is called multiple times in a second) and i also added a timer so it only updates every 1.3 seconds (this is the time that the download icon aniamtion takes to complete) and in this way i finally achieved my goal and the notification doesn't continuously rebuild.

Here is how i'm updating the notification:


while ((count = input.read(data)) != -1) {
if (Constants.stopService) {
output.flush();
output.close();
input.close();
stopSelf();
return;
}
if (count != 0) {
total += count;

latestPercentDone = (int) Math.round(total / lengthOfFile * 100.0);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
if (SystemClock.elapsedRealtime() - mLastClickTime > 1300) { // 1000 = 1second
mLastClickTime = SystemClock.elapsedRealtime();
sendMessage(percentDone, idLesson);
notification.setProgress(100, percentDone, false);
notificationManager.notify(realNotificationID, notification.build());
}
}
output.write(data, 0, count);
}
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();

Heads-up Ongoing Notification not hide

AFAIK, a notification needs the following properties to be set to be shown as a heads-up notification.

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)

setOngoing() means that the Notification is ongoing and cannot be hidden by the user. I believe that is probably why your Notification stays on the top and doesn't hide. Remove setOngoing(true) and it should work.



Related Topics



Leave a reply



Submit