non removable notification
Asked Answered
W

4

21

In my app, there is service running on background. I want to notify user that the service is running. But I need that user cannot delete the notification - by pressing clear button or by swipe it out, in notification bar

enter image description here

It means I need to show my notification above Notification area

Wrangle answered 17/11, 2012 at 2:53 Comment(0)
A
65

This is possible but the way you implement it depends on the API level you develop for.

For API levels below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:

// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis());

// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);

// THIS LINE IS THE IMPORTANT ONE            
// This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;

For API levels above 11, or when using the Android Support Library, one can implement it like this:

Notification noti = new Notification.Builder(mContext)
    .setContentTitle("Notification title")
    .setContentText("Notification content")
    .setSmallIcon(R.drawable.yourIcon)
    .setLargeIcon(R.drawable.yourBigIcon)
    .setOngoing(true) // Again, THIS is the important line
    .build();
Angle answered 17/11, 2012 at 3:22 Comment(2)
In combitation with antew's post, I was exactly looking for Notification.FLAG_ONGOING_EVENT. Thanks a lotWrangle
can you try this android 8.1.0 is it working seems its not work ingGeary
H
13

To Create Non removable notification just use setOngoing (true);

NotificationCompat.Builder mBuilder =
                         new NotificationCompat.Builder(this)

                        .setSmallIcon(R.drawable.ic_service_launcher)

                        .setContentTitle("My title")

                        .setOngoing(true)  

                        .setContentText("Small text with details");
Heard answered 3/4, 2014 at 9:50 Comment(1)
Have you checked in android 14 OS device ?Disorientate
O
3

Sound like Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT is what you are looking for.

Oxbow answered 17/11, 2012 at 3:21 Comment(0)
D
2

To Create Non removable notification just use setOngoing (true);

To Create removable notification just use setOngoing (false);

Determinative answered 4/6, 2018 at 10:27 Comment(1)
It will not working in android 14 OS deviceDisorientate

© 2022 - 2024 — McMap. All rights reserved.