Notifications visible under API 17, but invisible under API 13 - android

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?

Related

Android push notification icon LIght theme issue

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

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.

What size can we use for the notification badge icon in FCM?

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)

Change the icon dynamically in android notification

My icons are having names icon_1, icon_2, icon_3 and so on. And I want to change the icon in the notification dynamically according to the input. The input is a number ranging from 1 to 100.
if the input is 1 then icon_1 should be shown, if input is 2 then icon_2 will be shown and so on. Is it possible to set the icon in one line or we are forced to use switch case statement? The example with the code I am pasting here to better understand. Switch case statement definitely will help out but only want to know if it is possible to write in one line to save 100 lines of code.
The following lines of code may not work. But just for understanding the things, I have used.
Input is a number in the variable name of num.
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentText("Subject")
.setSmallIcon(R.drawable.icon_+"num") //Here is the doubt..How can we modify this line to work
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager=NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
Have a look at this
//create a array of your notification icons
int[] not_icon={R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3.......so on};
//pass the array accordingly to your input or payload
.setSmallIcon(not_icon[3]); //3 is the number you received in your payload.

Android Jelly Bean Strange behaviour of notifications

I have integrated notifications in my app, and I have handle 2 cases :
Pre Jelly bean notifications with NotificationCompat.Builder, and,
Post Jelly bean notification with builder.
This makes me able to manage big text, and actions in post jelly bean version, and works for 2 or 3 time, but in a strange way, today I got the same result in JB and below.
Code for Pre JB :
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pint = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder notif = new NotificationCompat.Builder(context)
.setContentTitle(nMessage.getTitle())
.setContentText(nMessage.getMessage())
.setTicker(nMessage.getTitle())
.setWhen(when)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_ttd_petales)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setContentIntent(pint);
notification= notif.build();
notificationManager.notify(0, notification);
Code for JB and above :
Builder bigTextNotification = new Notification.Builder(context)
.setContentTitle(nMessage.getTitle())
.setTicker(nMessage.getTitle())
.setContentText(nMessage.getMessage())
.setWhen(when)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_ttd_petales)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(pint);
if(nMessage.getHasPhone()){
Intent iCall = new Intent(Intent.ACTION_CALL,Uri.parse(nMessage.getPhone()));
PendingIntent pintCall = PendingIntent.getActivity(context, 0, iCall, Intent.FLAG_ACTIVITY_NEW_TASK);
bigTextNotification.addAction(R.drawable.ic_menu_call, context.getResources().getString(R.string.call_ttd), pintCall);
}
String[] recipients = new String[]{context.getResources().getString(R.string.default_email)};
String subject = context.getResources().getString(R.string.about_offer);
Intent iEmail = new Intent(android.content.Intent.ACTION_SEND);
iEmail.setType("text/html");
iEmail.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
iEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
PendingIntent pintEmail = PendingIntent.getActivity(context, 1, iEmail, Intent.FLAG_ACTIVITY_NEW_TASK);
bigTextNotification.addAction(R.drawable.ic_menu_compose, context.getResources().getString(R.string.call_ttd), pintEmail);
Notification notif = new Notification.BigTextStyle(bigTextNotification)
.bigText(nMessage.getMessage())
.build();
notificationManager.notify(1000, notif);
Have you ever encountered such behaviour or am I missing something?
I had a similar experience: I would send the notification and it would show the content text instead of the big text. Then when I unplugged the USB, it would show the big text expanded.
Now notice that when you plug in to USB, you get an ongoing notification: "Connected as a Media Device". Take a look and see if you get the same behavior with some other ongoing notification, like "Searching using GPS". I do. (My test device is a Samsung Galaxy Rush, and its vertical space is severely limited.)
So while the Android Notifications documentation says that only the top notification shows as expanded, it looks like there's another rule as well:
If the notifications list is running out of room, it will collapse even the top notification.
You can expand the collapsed notification by double- or single-finger dragging, depending on the OS version.
I'm sorry for this dummy question, I'm not using emulator to test this app, but real phone, and the strange thing is that when usb debugging cable is plugged, there's no actions in the notifications (JB) and when unplugged, we get our buttons working :) this solves my issue, but I have no idea about this behaviour.

Categories

Resources