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
Related
I am using FCM push notification for my app, everything is working fine. but right now, I am getting a small icon below my notification icon. See below image.
Can any one tell me what is the size we can use for this icon? And how can we customize it?How can i add icon or image at that place?
You can use badge icon of any size like 72x72 or any
but the thing is your icon should be .png extension only of transparent background and icon color should be white only... Here is the code for setting large notification icon and badge icon
String title = context.getString(R.string.app_name);
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.tlogo)
.setLargeIcon(bm)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(when);
The icon in the image suggests that the notification has come from Chrome which recently added support for badge customisation in version 53.
There is sadly no guidance on the recommended size, but a square image with transparent background and only white would be the recommendation I can give.
72x72px is probably a good bet for image size based on Android docs which suggest a badge size of 24dips (which can be thought of as 24px multiplied by a devices screen density). So 24px x 3 = 72px.
To set the badge on a web notification you need to include a badge options:
self.addEventListener('push', function(event) {
event.waitUntil(
self.registration.showNotification(
'Hello', {
body: 'Thanks for sending this push msg.',
icon: './images/icon-192x192.png',
badge: './images/icon-72x72.png'
})
);
});
Info from: https://medium.com/dev-channel/custom-notification-badges-for-all-df524a4003e8#.jwbxe7eft
Further Info: https://web-push-book.gauntface.com/chapter-05/02-display-a-notification/#badge
I tried this and it solved,sorry for late answer
private void sendNotification(String messageBody,String titles) {
Intent intent = new Intent(this, NotificationList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.appicon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(titles)
.setLargeIcon(largeIcon)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(getNotificationIcon())
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
return useWhiteIcon ? R.mipmap.notismall : R.mipmap.appicon;
}
The icon size is not stated in the Firebase reference documentation for the notification payload used to send messages to client apps, however you can check out the guidelines in creating icons for Android and IOS.
So I did some digging about Android Notifications and Building Notifications. You can set the icon by using the icon parameter.
However, for the Badge inside the notification, this one seems to be handled by the Android System itself and cannot be modified. Depending also on the device, it may very well be that the badge is not visible, since Android itself doesn't allow Notification Badges even for App Icons.
So far, I've done some testing on my device and only the notifications that show badges are the ones that can be opened by Google Apps (Gmail, Drive, Photos, etc.) and the notifications sent by Device Manufacturer.
The one you've included in your post must be a default badge made by the device manufacturer and it is only showing because of the Device's App Launcher.
So long story short, it's not possible for you to set the Notification's Icon Badge.
if you want to hide the small icon on top of large icon use
int smallIconId = mContext.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
Notification notification = notificationBuilder.build();
if (smallIconId != 0) {
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
you can even play more with findviewById(smallIconId)
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.
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.
I want to create a custom notification. So i want to change the lights and the tone.
I use the NotificationCompat.Builder for that.
Now i want to change the Lights via setLights();
Works fine. But i want set the default value of the onMS and offMS. I haven't find something about that.
Can anybody help me to find the default values?
Here is the documentation for that: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)
See the Android source for answer:
<!-- Default color for notification LED. -->
<color name="config_defaultNotificationColor">#ffffffff</color>
<!-- Default LED on time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOn">500</integer>
<!-- Default LED off time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOff">2000</integer>
However different ROMs might have different values for these. For example mine returns 5000 for config_defaultNotificationLedOff. So you might want to fetch them at runtime:
Resources resources = context.getResources(),
systemResources = Resources.getSystem();
notificationBuilder.setLights(
ContextCompat.getColor(context, systemResources
.getIdentifier("config_defaultNotificationColor", "color", "android")),
resources.getInteger(systemResources
.getIdentifier("config_defaultNotificationLedOn", "integer", "android")),
resources.getInteger(systemResources
.getIdentifier("config_defaultNotificationLedOff", "integer", "android")));
According to diff, these attributes are guaranteed to exist on Android 2.2+ (API level 8+).
#Aleks G
that do not help. I have the latest update from the compat libaray. But Eclipse say build() isn avalaible.
I dont no why. The docu says yes and you...
This is my current code:
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setLights(Color.parseColor(led), 5000, 5000);
notify.setAutoCancel(true);
notify.setSound(Uri.parse(tone));
notify.setSmallIcon(R.drawable.ic_stat_kw);
notify.setContentTitle("Ttiel");
notify.setContentText("Text");
Intent showIntent = new Intent(context, Activity_Login.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, showIntent, 0);
notify.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.getNotification());
runs perfectly. But not with the default onMS and offMS in setLights() :(
You should be able to do this with:
Notifictaion notf = new Notification.Builder(this).setXXX(...).....build();
notf.ledARGB = <your color>;
notf.ledOnMS = <your value>; //or skip this line to use default
notf.ledOffMS = <your value>; //or skip this line to use default
Basically, don't use setLights on the notification builder. Instead, build the notification first - then you have access to individual fields for the lights.
Update: this is the actual copy/paste from my sample project, which compiles and works fine on android 2.1 and uses blue colour for LED:
Notification notf = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setTicker("This is the sample notification")
.setSmallIcon(R.drawable.my_icon)
.build();
notf.ledARGB = 0xff0000ff;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notf);
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?