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
What's the Difference Between Setwebviewclient VS. Setwebchromeclient
How to Make a Smaller Ratingbar
Remove Application Icon and Title from Honeycomb Action Bar
How to Draw Arc Between Two Points on the Canvas
Does the Android Art Runtime Have the Same Method Limit Limitations as Dalvik
Android Studio/Emulator on MACos with Arm CPU M1
Detect Viewpager Tab Change Inside Fragment
Android 5.0 Material Design Style Navigation Drawer for Kitkat
Disable Logcat Output Completely in Release Android App
Drag and Drop Items in Recyclerview with Gridlayoutmanager
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices
Android Share Intent for a Bitmap - Is It Possible Not to Save It Prior Sharing
Overlay Images Onto Camera Preview Surfaceview
Jar Mismatch! Fix Your Dependencies
Webview Showing Err_Cleartext_Not_Permitted Although Site Is Https