NotificationCompat.Builder addAction in pre JellyBean Androids? - android

I have a notification with a addidional action so user is able to go to the main activity by clicking the main notification or stop the service clicking the secondary (extented) notification. This works fine for post JellyBean Androids (4.1+). How to achieve similar functionality in older systems (API 11+)?
noti = new NotificationCompat.Builder(this)
.setContentTitle("service")
.setContentText("running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "Stop", pIntent)
.build();
startForeground(12345, noti);

From the docs:
Builder class for NotificationCompat objects. Allows easier control
over all the flags, as well as help constructing the typical
notification layouts.
On platform versions that don't offer expanded notifications, methods
that depend on expanded notifications have no effect.
For example, action buttons won't appear on platforms prior to Android
4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.
For this reason, you should always ensure that UI controls in a
notification are also available in an Activity in your app, and you
should always start that Activity when users click the notification.
To do this, use the setContentIntent() method.
and from the addAction():
Add an action to this notification. Actions are typically displayed by
the system as a button adjacent to the notification content. Action
buttons won't appear on platforms prior to Android 4.1. Action buttons
depend on expanded notifications, which are only available in Android
4.1 and later. To ensure that an action button's functionality is always available, first implement the functionality in the Activity
that starts when a user clicks the notification (see
setContentIntent()), and then enhance the notification by implementing
the same functionality with addAction().
I found this tutorial that you might find useful.

Related

Customizing full screen intent notifications android

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.

Android bundled notifications behaviour

Android N introduced Bundled notifications:
posting 4 or more notifications without a group will cause those
notifications to be automatically bundled.
I'm creating four very similar notifications with different tag and id but with the same icon. After notifying NotificationManagerCompat and creating 4rd notification, all notifications are bundled into one with my application name and grey icon. And this leads to strange behaviour because it looks like that icon is greyed out shape of my applications launcher icon instead of smallIcon from NotificationCompat.Builder
I created sample app and tried to simulate the same environment, and created 4 notifications using same builder methods as in first application. Instead grey shape, I can see original smallIcon from builder which is correct behaviour.
On the screen, first notification is from my sample app, the second one is from my main app.
Creating notifications in both apps looks the same:
NotificationCompat.Builder notifBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.alert_octagon_white)
.setContentTitle("contentTitle 2")
.setTicker("ticker 2")
.setContentText("contentText 2")
.setStyle(new NotificationCompat.BigTextStyle().bigText("bigText 2"));
NotificationManagerCompat.from(this).notify("tag2", 2, notifBuilder.build());
Where can I look for the differences? How can I set bundled notification icon?
I am not sure of the difference in behavior you are seeing but i think something in Android Nougat default Notification bundling is screwed up.
To avoid that specifically call setGroup() and setGroupSummary() on your Builder object.That would solve the issue.

How do I display hidden notifications?

I am developing a simple app that starts a service when a button is tapped... The service create an ongoing notification but I don't want it to display any icon in the status bar...
In this picture you can see WiFi ADB has a standard ongoing notification that cannot be dismissed...
Google Now (The 62 Cloudy), Automatic, and Automate do not display any icon when the notification bar is closed and they are in a separate group (under that grey line).
I looked everywhere for how to achieve that but can't find anything... Even the Android documentation which is usually quite exhaustive doesn't provide any information about it.
EDIT:
Right now this is how I display my notification:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Power Napp")
.setContentText("Napping...")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
There's a simple trick. Add:
.setPriority(Notification.PRIORITY_MIN)
And the notification won't show the icon in the status bar.
http://developer.android.com/reference/android/app/Notification.html#PRIORITY_MIN
isn't very clear about this but it's documented here:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority

How to show Android Notifications on screen as well as status bar icon?

I feel like this should be trivial but I can't seem to make a notification show up on the phone's screen - it only shows up in the status bar at the top.
For an example of what I want to do, here's how Facebook Messenger shows up on the screen when you receive a message.
Whenever I send a notification, all it does is show the little icon in the status bar - even if I set the priority to PRIORITY_MAX. Is there another setting I need to do to make it show on screen instead of just status bar?
The Notification display code:
PendingIntent contentIntent = PendingIntent.getActivity(context, nextId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(largeIcon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(context.getResources().getColor(R.color.orange_500))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Notification notification = builder.build();
notificationManager.notify(id, notification);
All things considered, it's a really good idea to use NotificationCompat.Builder over Notification.Builder, let alone creating a Notification manually. It gives you nice backwards compatibility with graceful degradation (all the way back to API Level 4, otherwise known as "gadzooks, that's old"). AFAIK, it's the only way to get some of the Android Wear stuff going, when used in concert with NotificationManagerCompat. And, in this case, it seems to be happier with the newer Android 5.0+ features.
In this case, setPriority(NotificationCompat.PRIORITY_HIGH) on a NotificationCompat.Builder, used with NotificationManagerCompat, will give you the heads-up notification on Android 5.0+.
Another point, make sure the 'importance' of the notification channel you have set up for your notification is set to NotificationManager.IMPORTANCE_HIGH.
This configures how visually intrusive notifications posted to this channel are and 'high' will allow it to peek. If you have this set to NotificationManager.IMPORTANCE_DEFAULT then it won't!
Bear in mind when configuring a notification channel, once the code has ran on your device, you won't be able to alter this again. So if you need to change the importance you will need to uninstall the app and then re-run and you should see the notification on your screen!

Android Notification without click event?

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.

Categories

Resources