I am developing an application to blink the notification led for the events like inbox messages and missed calls based on the user choice in different colors.
When an inbox message is received then I will start blinking the LED (Using Notification.ledARGB).
But the problem is the LED is blinking with the default color(white - which is the default led color for messages) but not the color set by me/user.
How to avoid or override blinking the default message app color and show my LED color?
I wanted to provide more details regarding my question to get the better answer.
What I am doing is when i get a incoming message i am getting broadcast receiver and catching the action and showing my notification with some color. The notification is showing in status bar but the color is coming white color (may it is message app default color).
While by researching a lot I came to know that whichever app is raised the notification first that notification led color will blink till we cancel the notification by swiping from the notification bar or by clicking. If we rise our notification with our custom color it will not blink till that previous notification is canceled.So what I want to do is I want to stop blinking of the default color and start blinking the color set by me.
I am using LG Nexus 4 with Lollipop.
You must override all the Notification defaults by setting:
notif.defaults = 0;
Here's a snippet of code:
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.defaults = 0;
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(LED_NOTIFICATION_ID, notif);
}
Use setLights method for notification builder.
Notification.Builder notificationBuilder = new Notification.Builder(context);
notificationBuilder.setLights(Color.CYAN, 5000, 5000); //you can give your choice's color here.
Make sure to override defaults using this flag:
Notification notification;
notification.defaults = 0;
Hope this helps.
Related
I have an issue with hiding the notification icon in the status bar from a service notification.
Here what I did:
I set the notification priority to min, make them silents
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"test");
builder.setSmallIcon(R.drawable.notif);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(profile.mName);
builder.setContentIntent(pendingIntent);
builder.setNotificationSilent();
builder.setPriority(NotificationCompat.PRIORITY_MIN);
In the channel, I set the priority to min too but I still see the notification icon in the status bar
NotificationChannel channel = new NotificationChannel(id, getApplicationContext().getString(R.string.app_name), NotificationManager.IMPORTANCE_MIN);
channel.setSound(null, null);
channel.setImportance(NotificationManager.IMPORTANCE_MIN);
channel.enableVibration(false);
Alright, the thing I can understand that you first tried with priority or importance high with notification channel. According to documentation once notification channel created then it cannot be updated by changing code. If you want to apply new changes like priority or importance low like you did in code, then you need to clear app cache or uninstall app and then re-install again.This will apply new notification channel changes to your app.
I used to show a number in app icon using this library as follows:
ShortcutBadger.applyCount(context, numberToShow);
OneSignal also has same function in its Android SDK.
Now in Oreo, with the introduction of notification channels, things get complex to me. I can create a channel. Then, I can also create a notification as follows:
public static void createNotification(Context context, int numberToShow) {
Notification notification = new NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle("Dummy Title")
.setContentText("Dummy content")
.setSmallIcon(R.drawable.app_icon)
.setNumber(numberToShow)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notification);
}
However, I have to show a notification with this solution, which I don't need and thus don't want. Is there any way in Oreo that I can achieve the same thing I have done previously, i.e. just showing 'notification dot' or a number attached to the app icon?
Sorry, but there is no SDK-level support for showing numbers or other badges on launcher icons, other than the Notification scenario that you described.
set the importance of the notification channel to
IMPORTANCE_MIN
like int importance = NotificationManager.IMPORTANCE_MIN;
and then create the channel as -
NotificationChannel nChannel = new NotificationChannel
(channelId, title, importance);
This will the set the badge count(shown on the long press of the icon) without notifying the user about any notification in the system tray. Though the notification will be in the tray, but will not pop up and quietly reside there.
In earlier versions of Android, it was possible to have a notification that did not show an icon in the status bar, but was expanded by default by using the priority setting.
// Hide status bar icon AND show notification as expanded
builder.setPriority(NotificationCompat.PRIORITY_MIN);
However, now when targeting Android 25 (Oreo) on a device running Android 26, it seems this isn't possible. When I set the importance level to IMPORTANCE_MIN I achieve the desired effect of not showing an icon in the status bar. However, the notification is "collapsed" by default.
// Hide status bar icon (but notification is collapsed)
NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_MIN);
If I set the importance level to (or above) IMPORTANCE_LOW then the notification is expanded by default, but shows an icon in the status bar.
// Expand notification (but shows status bar icon)
NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
Is there any way to force the notification to be expanded by default when using IMPORTANCE_MIN? Perhaps I've missed some setting.
PS: Please do not suggest using setSmallIcon to transparent. That's just an old ugly hack that looks awful.
By "collapsed" and "expanded" I mean this:
Collapsed:
Expanded:
Source snippet
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
notifManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setContent(myRemoteViews);
builder.setSmallIcon(R.drawable.status);
builder.setOngoing(true);
builder.setTicker(tickerText);
builder.setPriority(NotificationCompat.PRIORITY_MIN);
Notification notif = builder.build();
notifManager.notify(NOTIF_ID, notif);
I know this question is asked so many time related to Push notification status bar icon, but my problem is a little bit different.
I am using FCM push notification, my notification is coming properly. I have set notification icon for pre-lollipop and more then lollipop version. My notification icon is showing properly in dark theme devices like MOTO-G 5+, but when I check the same notification on light theme device like Samsung Galaxy Edge 7.0, my notification icon turns white as per app theme, but in another app, the notification icon is looking properly. I saw many example solution, but they did not help with my problem.
I am using following code :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ila_app_icon);
long when = System.currentTimeMillis();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentText(message)
.setContentIntent(pendingIntent)
// .setColor(getNotificationColor())
.setSmallIcon(getNotificationIcon(notificationBuilder))
.setWhen(when)
.setSound(defaultSoundUri)
.setLargeIcon(bitmap);
Random random = new Random();
int uniqueIntegerNumber = random.nextInt(9999 - 1000) + 1000;
notificationManager.notify(uniqueIntegerNumber, notificationBuilder.build());
Get notification icon method :-
private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int color = 0x008000;
notificationBuilder.setColor(color);
return R.drawable.notification_white;
}
return R.drawable.ila_app_icon;
}
I also tried setColor property, but that also doesn't work.
1) Light Theme ( Here the white flag icon )
Here is the screen shot :-
2) Dark Theme :-
After all searching and other things i found my solution,everything was working well expect the notification icon, the icon was keeping little bit pixel of different color.
We can create the notification icon from the following link, it makes our icon transparent for notification
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit
I am using NotificationCompat.Builder to make a simple notification, which is then added to the notification bar. On an AVD running API 17, the notification can be seen in the notification pull-down as expected. But on an AVD running API 13, the title/text/info of the notification cannot be seen (possibly due to black text against the black background).
In searching for a solution to my problem, I found the following: Custom notification colors and text colors But this seems to apply only in the case of using a custom layout for the notification.
Here is my code:
private static int notificationId = 1;
private void displayNotification() {
// create an Intent to launch the Show Notification activity
// (when user selects the notification on the status bar)
Intent i = new Intent(this, ShowNotificationActivity.class);
// pass it some value
i.putExtra(ShowNotificationActivity.NOTIF_ID, notificationId);
// wrap it in a PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, i, 0);
// create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("This is the Title #" + notificationId);
builder.setContentText("This is the Text");
builder.setContentInfo("This is the Info");
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(android.R.drawable.ic_notification_overlay);
builder.setAutoCancel(true);
Notification notif = builder.build();
// display the notification
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(notificationId, notif);
notificationId++;
}
I had hoped to post some screen shots showing the notification pull-down under API 17 and under API 13, but apparently I don't have sufficient 'reputation points' to post images. So I guess a written description will have to suffice:
Under API 17, the notification pull-down shows the notification's icon (a red dot), title, and text. The background color is black, and the text color is white.
Under API 13, only the icon (red dot) is visible. The background color is black, I suspect the text color may be black as well.
I suspect that I am missing something obvious, but I am new to this and would appreciate any pointers.
When I copy-pasted your code into my empty activity I got several errors(it seems your code is incomplete). I added these lines to make it work:
int notificationId = 1;
Intent intent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Here goes your code.
I just tested your code on a 3.2 emulator and this is what the notification looks like:
Which, I believe, is what it should look like.
Coult you test with my code and let me know if it works?