NotificationCompat.Builder setLights(); not working - android

I'm trying to use the LED on my notification and it's not working, i have this code:
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext());
nBuilder.setLights(Color.CYAN, 1000, 500);
The entire notification is working, like ContentTitle, ContentText and the notification is shown, but only the LED is not working.
Is there anything wrong with my code ? Should i use the Notification instead NotificationCompact.Builder ?

The LED light for notifications is turned on by the OS in the device only if the notification is triggered while the device screen is off.

Your code can not work, because you have to pass three variables:
the color
if turning on the led is on
if turning off the led is on
if you enable 2 and 3 your led will be blinking, if you disable 2 and 3 the led will be turned off
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
mBuilder.setLights(Color.RED, 1, 1); // will blink

That code seems OK, for the APIs before 26.
You might try adding Notification.FLAG_SHOW_LIGHTS
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);
nBuilder.setLights(Color.CYAN, 1000, 500);
Notification notif = nBuilder.build();
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notif);
Also make sure that you do not swipe down to preview the notification, otherwise the LED will be off when the display goes black.

Related

Rich Push Notification not hiding the large icon on expanding the notification containing image

I am following the android developers docs to create an expandable notification containing one image.I have written the same code as per the docs but my large icon is not hiding after I expand my notification.
Here's my code:-
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, id)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setContentTitle(title)
.setContentText(data.get("message"))
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)
.bigLargeIcon(null))
.setAutoCancel(true).setChannelId(id)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Notification notification = notificationBuilder.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.flags |=
Notification.FLAG_AUTO_CANCEL; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE;//Vibration
notificationManager.notify(mUniqueId, notification);
and android developer's code :-
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
.setSmallIcon(R.drawable.new_post)
.setContentTitle(imageTitle)
.setContentText(imageDescription)
.setLargeIcon(myBitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(myBitmap)
.bigLargeIcon(null))
.build();
I am not able to understand whether I am missing something or doing it in wrong way. Please help . Thank You in advance.
I got the answer !!
Actually it is device specific issue.Some android devices are not hiding the large icon on expanding the notification and some devices are hiding it.

NotificationManager.notify does not create a notification in one app, despite the exact same coding working in another app

I am simultaneously developing two Android applications that are communicating with each other, and I am using notifications to show received messages. This is the code I am using to show a notification:
private void showNotification(String title, String content) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"NOTIF_CHANNEL",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("CHANNEL FOR INFORMING ABOUT MESSAGE RECEIVED");
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "default")
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(title) // title for notification
.setContentText(content)// message for notification
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) // set alarm sound for notification
.setAutoCancel(true); // clear notification after click
Intent intent = getPackageManager()
.getLaunchIntentForPackage(getPackageName())
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(0, mBuilder.build());
}
In one of my applications, this works flawlessly every time, but in the other it never works. I am using the exact same code and running the applications on the same device.
I am curious as to if someone is able to identify or make a guess on factors that would make a difference here. I have tried using the same icon, title, content, and sound, but to no avail. Any help or suggestions would be greatly appreciated!
If your code is working in one app but not in the other, and the code is exactly the same, surely you are using distinct versions of support library and/or distinct targetSdkVersion. Set the same of the app which works in the other that doesn't works.

Android heads-up notification not showing

I am trying to make heads-up notification work. Notification is created, but it's not displayed at the top of the app.
Here's the code responsible for building a notification:
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Check running time - click!")
.setContentText(String.valueOf(elapsedTime))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0])
.build();
The device that I'm trying to run the app is API 21. I've seen many threads, but no solution given works for me.
You need to do two things:
Make sure that your notification is properly configured. See: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up
AND make sure the phone is properly configured (I think this is where most get stuck).
Step 1. Configure the notification.
First, register your notification channel like so
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Then, create a notification, like so:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
.setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification
Finally, send the notifciations as you would do normally, e.g.:
Notification buildNotification = mBuilder.build();
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, buildNotification);
Step 2. Configure the phone.
I noticed that I have to enable some additional settings on my phone (a Xiaomi Note 3):
Here are some ways to reach the menu:
Long press a notification, in the notification bar.
Go to: Settings > Installed apps > Select your app > Notifications
Improving on the previous step, you can help users partially by sending them to the installed apps menu, by using this intent:
startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
Finally, when you reach this menu enable a setting called something like "Floating notification" (the name of this setting varies between devices).
your code is almost fine. I'm using DEFAULT_VIBRATE instead of DEFAULT_ALL:
builder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
}
But, you can also set
builder.fullScreenIntent(sameAsContentPendingtIntent);
but ^ this one is non-deterministic. I mean that system choose to display a HeadsUp or launch Intent. In most cases it shows HeadUp, but I'm not counting on it because of Android versions, Manufacturers, launchers and so so on.
Not at last, you also need to define right Notification Channel. I think you had already done this, because you wouldn't see any notification if you don't :-) Oh lovely Android :-) Anyway, I want to say that also NotificationChannel needs to be in high priority:
channel = new NotificationChannel("uniqueId", "name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
And also, I advise to you, check latest opinions at developer.android.com
Google is going to be more strict from now. Not only for notifications but also for 'targetApi', but that is another story, pardon me :-)
Have a nice code today
try do this :
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Check running time - click!")
.setContentText(String.valueOf(elapsedTime))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0]);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
I've encountered the same issue. The solution is simply properly setting the vibration builder.setVibration(NotificationCompat.DEFAULT_VIBRATE).
According to Google:
The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
I hope it helps :)
The answers provided above were correct, but are partially correct now.
For future readers, notice that
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
will NOT resolve the problem.
According to "Android for developers",
https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
Example conditions that might trigger heads-up notifications include the following:
The user's activity is in fullscreen mode (the app uses fullScreenIntent).
The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.
Notice that the second solution (which is mostly mentioned under this question), only works for Android 7.1 or lower.
For Android 8.0 and higher, you should create a notification channel whose importance is NotificationManager.IMPORTANCE_HIGH.
Some sample code:
NotificationChannel channel =
new NotificationChannel(
"MY_OWN_CHANNEL_ID",
"MY_OWN_CHANNEL_NAME",
NotificationManager.IMPORTANCE_HIGH);
and after that, make sure that your notification builder uses this channel to display
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(smallIcon)
.setContentTitle("Title")
.setContentText("Content")
.setChannelId("MY_OWN_CHANNEL_ID")
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0]);
Now you should be able to see heads-up notifications properly.
Just use like
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, MainActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ttile")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());

Why do notification SOUNDS make the phone VIBRATE?

If I create a regular notification without sound, it works correctly. But if I add a sound, it vibrates. This is illogical so I don't understand it.
Here is an example of the code that works correctly (doesn't vibrate)
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
.setOngoing(false);
builder.setCategory(Notification.CATEGORY_SERVICE);
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setLights(0xFF00FF00, 1000, 0);
Uri theUri = Uri.parse(uriString);
//builder.setSound(theUri);
builder.setContentText("Android sucks");
builder.setAutoCancel(false);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(FOREGROUND_ID, builder.build());
Here is the code that causes the phone to vibrate. Notice that vibration is NOT set in my notification:
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
.setOngoing(false);
builder.setCategory(Notification.CATEGORY_SERVICE);
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setLights(0xFF00FF00, 1000, 0);
Uri theUri = Uri.parse(uriString);
builder.setSound(theUri);
builder.setContentText("Go buy an iPhone");
builder.setAutoCancel(false);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(FOREGROUND_ID, builder.build());
Call me irrational, but when I want the sound I'll set the sound and when I want vibration, I'll set the vibration. If I want both, I'll set both. So why is it that when I set the sound, I automatically get vibration?
This seemed to do the trick:
long[] nullify = {0,0,0};
builder.setVibrate(nullify);
Although it lead to a number of additional frustrating anomalies. There are seven audio streams. They are:
STREAM_RING;
STREAM_NOTIFICATION;
STREAM_ALARM;
STREAM_MUSIC;
STREAM_DTMF;
STREAM_SYSTEM;
STREAM_VOICE_CALL;
Now, if I had to choose which stream I thought the notification sound would use, which stream do you think I'd guess? You are correct, I expect it to use STREAM_NOTIFICATION. What stream does it use? STREAM_SYSTEM. So if I want to be able to control the volume of my notification, I have to set the volume for STREAM_SYSTEM. But that of course has unwanted side effects like now that I changed that setting you can hear an audible sound when I click "like" on something in the facebook app, for example.
Android is crazy messed up.

SmallIcon Color Turn To White Automatically Why In Notification Android?

public void animatedNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(ic)
.setLights(Color.GREEN, 10000, 10000)
.setWhen(when)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(title)
.setContentText("Plants Need Watering Some of Your work is pending");
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
.setSmallIcon(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? R.mipmap.ic_launcher : R.drawable.notification_icon_bw_xhdpi)
.setColor(ResourcesCompat.getColor(getResources(), R.color.primary, getTheme()))
Use color to distinguish your app from others. Notification icons should only be a white-on-transparent background image. (https://developer.android.com/design/patterns/notifications.html)
Before Android Lollipop you can use same mipmap icon for all notification icon. But from Lollipop onwards you need to create new notification icon (Silhoette kind of icon).
Small icon appears white because of Material theme used for your app notification.

Categories

Resources