I can't remove my app notification from notification bar by sliding/swiping or with default "clear all" option in android.
I got .setAutoCancel(true) in code as well.
But it seems that doesn't work.
anyway to make it removable by swiping like I can do on other app notification. Mine just stuck there.
I know the cancel() and cancelAll(), and giving an intent method to remove by touch (from other stackflow posts).
I just found an help in another stackflow post..(was opposite of my requirement).
For me, I needed to make false in ongoing
nBuilder.setOngoing(false);
where, nBuilder is my Notification.builder
Related
Project Target API 30 Android 10, Min API 19 KitKat
I am creating a parental control app where parents can restrict certain apps.
I have a foreground service where I would I ideally trigger an activity-like notification from the service that would envelop the user's entire screen or take them to the home screen.
I have learned that starting activities, including going to the home screen, is no longer possible under normal circumstances as of API 29. https://developer.android.com/guide/components/activities/background-starts
After reading the documentation it seems creating a full screen intent notification is the most highly recommended workaround to the activity restriction.
I am currently working with the following code for my full screen intent notification:
Intent blockedIntent = new Intent(App.getContext(), BlockedItemReceiver.class);
blockedIntent.putExtra("currentApp", restrictedApp.name);
PendingIntent pendingBlockedIntent = PendingIntent.getActivity(App.getContext(), 50, blockedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder blockedNotificationBuilder = new NotificationCompat.Builder(App.getContext(), CHANNEL_BLOCKED_FROM_ITEM)
.setSmallIcon(R.drawable.ic_baseline_lock_24)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle(restrictedApp.packageName + " was Blocked!")
.setContentText(restrictedApp.name + " will be available again DAY at TIME")
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(pendingBlockedIntent, true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(5, blockedNotificationBuilder.build());
I looked at some documentation for customizing notifications, but the information I found does not have a full screen intent notification example, and when I attempt to add the methods in the documentation example for NotificationCompat.Builder such as setCustomContentView() with custom layouts, my notification fails to appear without an error message.
https://developer.android.com/training/notify-user/custom-notification
Besides, I do not want a collapsed and expanded version of my notification as the example in the link has, just one full screen view.
TLDR; I want a notification that always engulfs the screen until the user presses a button on the notification to dismiss it. How can I further customize my full screen intent notification to truly take up the full screen? Ideally with a layout. If I must have a collapsed version of my notification, I don't want the user to ever see it, because I always want my notification to engulf the screen while it's showing.
There are existing apps such as AppBlock that have found a workaround to launching an activity-like thing from a foreground service that takes up the full screen, so what I am trying to do is possible even if the specific question I'm asking won't lead me to that result. Please suggest another way of accomplishing what I am after if what I am asking to do in my question is "impossible". What I generally want to do is certainly possible.
I want to remove the ongoing notification from statusbar.For example whatsapp web notification when whatsapp web is active,usb debugging notification when device is connected to pc which are not removed when swipe.Is it possible or not?If its possible then please help me.Thank u.....
Yes, you can dismiss a notification from statusbar. Just call cancel() for your notification ID.
Check out this link to learn more about removing Notifications: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Removing
If you are hiding only the notification icon on the notification bar (glance view without pulling down the slide bar to see the whole on going notifications), and its notif view still in the on going list, you can pay attention to the method setPriority of NotificationCompat.Builder.
In my case,the codeblock below saved my day!
NotificationCompat.Builder mBuilderOnGoing = new NotificationCompat.Builder(mContext);
if (Build.VERSION.SDK_INT >=16) {
mBuilderOnGoing.setPriority(NotificationCompat.PRIORITY_MIN);
}
Hope it helps
After a long search, I was not able to find exactly what I need.
I simply want : When a notification is displayed to the user, if the app is simply in background I want to reopen MainActivity. But if the app/activity has been killed, I want to restart the app completely.
My actual code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(notification.getSubject())
.setContentText(notification.getMessage())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentIntent(
PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT));
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = mBuilder.build();
notif.flags = Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
Case which is not working actually is to restart the app if app or activity has been killed.
EDIT : By killed I mean the app has been closed in the app manager. Or we lost the focus and the activity is destroyed. In those cases I would like to fully restart the app.
I think you don't entirely understand how android functions. Whenever your program is needed, the program is started by android (See the Application class for an onCreate that you can override).
Whenever an activity is necessary it is (re)created or brought to the foreground.
That means that an activity restart does not necessarily imply a restart of the application. In practice, if the program was still in memory, then an application restart won't happen.
If you want to investigate this further
create an Application.onCreate method so you see whether your app is restarted or not.
trigger the notification
go to the android settings, application tab, and there select 'show cached processes'. Clear your process from the list.
tap the notification.
This should trigger a restart of the application. If this is indeed the problem then you cannot 'solve' it. Android decides when it will kill your application and when it removes it from memory.
The best solution then is to redesign your activity that whatever the application restart would trigger is also performed in the activity itself. Or so, without more detail what functionality you would like to see performed on an 'activity/application-restart' it is difficult to advice further on this matter.
I think you need to change your setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) value to setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).
The reason is due to the way that FLAG_ACTIVITY_REORDER_TO_FRONT
works. When you use this flag, Android looks for an instance of the
desired activity in your activity stack, starting from the front of
the stack and scanning until it gets to the root/back of the stack. As
soon as it finds an instance of the specified activity, it brings that
one to the front (ie: if there are multiple instances of the specified
activity it will bring to the front the most recent instance).
Original I found here.
See android developer doc here.
I hope its help you.
I need to remove the notification.without affecting the foreground service of the application.thanks in advance
Notification note = new Notification(R.drawable.ic_blank,"",System.currentTimeMillis());
Intent i=new Intent(this, MyLocationListener.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi=PendingIntent.getActivity(this, 0,i, 0);
note.setLatestEventInfo(this, "","", pi);
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground(42, note);
Android OS don’t like you to do this because users are entitled to know you are running a foreground serivce on their devices.
But if you must remove notification of foreground service :
In order to remove the notification icon in the notification area (the status bar) while foreground service still running :
Just set the priority to minimum (-2) in the notification builder:
for example:
/* (Notification.PRIORITY_MIN) will remove the notification in statusbar */
builder.setPriority(Notification.PRIORITY_MIN)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Service Started")
.setTicker("Music Playing")
.setWhen(System.currentTimeMillis());
This will only remove the small notification icon in the notification area.
if you also need to get rid of the detail notification rectangle in the notification drawer :
then what you need to do is more complex:
you need to start your service as foreground, then start another foreground service with the same notification ID as you have in your original service.
Then close ( stopself() ) the new foreground service, and Android system will remove the notification (while your original service that started previously will stay in foreground without the notification).
This works fine in 5.1.1, I don’t know if android team already close this breach in marshmallow .
BTW:
In order to do this there is also a non-programmatically way :
Go to settings -> applications -> application manager find your application and press on it.
You will get inside your application info.
Disable the “show notifications” option in your application info.
This will get rid of all notifications for your app but it also disable toast messages..
I don’t think there is a way to disable this option in settings programmatically from inside the application - I think android prevent it for security reasons. If anyone knows how to change this programmatically please tell..
if while trying to avoid the notification detail rectangle in the drawer you will remove these lines in your notification builder:
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Service Started")
.setTicker("Music Playing")
.setWhen(System.currentTimeMillis());
Then Android system will keep on showing a notification rectangle about your service (with the title “This service is running, touch for more information or stop the service ” ) and pressing on this rectangle will lead the user to Your application info on settings -> applications -> application manager with option to “force stop” this service.
Regarding that you can read more here https://plus.google.com/105051985738280261832/posts/MTinJWdNL8t
hope it helps :-)
Adding this line builder.setPriority(Notification.PRIORITY_MIN) will remove the notification icon from the status bar and lock screen.
Also removing or commenting builder.setSmallIcon(R.drawable.ic_launcher) removes the notification icon even when you scroll down notifications when device is unlockedbut i m not sure how it will work in android N
I would like to try and make a Notification that does not respond to a click. Is this possible? i.e. when it is visible in the Notification area, and the user touches it, I want "nothing" to happen.
This is different to a normal Notification - when it is visible, and the user touches it, the Notification Area hides, and the Intent is launched. I do not want the Notification Area to hide when it is touched.
I can make one that doesn't launch an activity:
Possible to make an Android Notification that does not call an Intent?
Android - notification manager, having a notification without an intent
But it still responds to a click, by hiding the notification bar again (i.e. standard expected behaviour).
This question is a duplicate of How to disable a click event of notification in android development but that question does not have an answer. (if there is a better way to ~bump~ that question up again, I will do that & delete this question)
Note: I know this is not "the Android way", but it is still something I would like to try.
I do not want the Notification Area to hide when it is touched.
That would require firmware modifications. You are not in control over the notification area; the OS is.
Use Intent intent = new Intent() and insert this into the PendingIntent.
Have you ever tried
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent null or empty and autoCancel false
.setContentIntent(null)
.setAutoCancel(false);
For more info about setAutoCancel() check Android Document here.