I am using the below snippet to generate notifications in my android app.
private void sendNotification(String contentText, String message) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("clear","clear");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent,0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(getApplicationContext(),R.color.red))
.setContentTitle("title")
.setContentText(message)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
,R.drawable.notification))
.setContentIntent(piResult);
NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
int i;
for(i=0; i<messageList.size();i++){
notification.addLine(messageList.get(i));
}
notification.setBigContentTitle("title");
notification.setSummaryText(contentText);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,notification.build());
}
It works in android 5 and 6 but for android nougat it is not working
Following the docs from the Android API:
Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate.
Your whole image but the transparent parts is going to be converted to white (being originally white or having colors).
One solution is to create a silhouette icon with color, by this way you can use the same image in all Android APIs. One example could be this icon:
In lower versions of Android you would see the black face, in the last versions you will see the same face but with white color. That's because of the transparent parts (it seems SO removes the transparency, you can get the original from here) of the image.
From android version lollpop onwards, they have made the changes for the notifications. When you are specifing the small icon, it should be of specific size as mentioned in this link.
The important thing is the image should be transparent and contains only white color.
You can check this question to get the answer
According to this blog here
It says that
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
If you’ve been building your notification with NotificationCompat.Builder and the standard styles available to you there, you’ll get the new look and feel by default with no code changes required.
I've got a strange problem with notification icon today.
It looks like this :
(the white circle ...)
Did I do something bad ?
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon_notification)
.setContentTitle(this.getString(R.string.notification_title))
.setContentText(this.getString(R.string.notification_text))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Here is my icon image (freshly downloaded from here https://material.io/icons/#ic_photo) :
http://image.noelshack.com/fichiers/2016/44/1478185219-icon-notification.png
Did I miss something ?
For the record, I'm using SDK 24 and only created the hdpi resource folder for now.
Edit #1 : I've added the ldpi, mdpi and xhdpi icons, nothing change ...
Edit #2 : For more precision, I'm trying to create this notification from a service ... FCM messaging service ...
If your compileSDKversion is above 20 then notification icon should be a white-on-transparent background image. Otherwise the image will be rendered as a white colored image.
Please go through the below link too for guidelines to create the icon
https://www.google.com/design/spec/patterns/notifications.html
and also the notification icon generator.
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.space.trim=1&source.space.pad=0&name=ic_stat_example
You must use a notification icon with no background. Android will add the circle background.
You can set background color with
.setColor(context.getResources().getColor(R.color.colorPrimary))
to match your app indentity.
Icon inside will remain white and circle will get the color you defined.
It seems to be a problem of cache during compilation ... The first image I was using was bad (fully colored), so I think my compilator created somekind of cache on the filename.
I work on Windows and did this : uninstall the app from my phone, invalidate all cache from Android sudio => at re-compilation, the icon was OK.
U need to have separate icon generated which will be white version of your launcher icon. U can use below link to generate such icon.
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
Note : You need to upload PNG image of your launcher icon with transparent background.
For setting icon u can have a method like this
private int getSmallIconForNotification(){
return (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP)? R.mipmap.ic_stat_launcher : R.mipmap.ic_launcher;
}
Code usage:
private NotificationCompat.Builder createNotificationBuilder(){
return new NotificationCompat.Builder(this)
.setSmallIcon(getSmallIconForNotification())
.setContentTitle("New Message")
.setContentText("Hi there.....")
.setAutoCancel(true);
}
I had the same problem while the app is closed and this helped me
Unfortunately this was a limitation of Firebase Notifications in SDK 9.0.0-9.6.1. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.
With SDK 9.8.0 however, you can override the default! In your AndroidManifest.xml you can set the following fields to customize the icon and color:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/google_blue" />
Note:- If device has android version above 20 you have to generate icon with transparent background and while generating notification use this snippet
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion=R.mipmap.ic_notification_lolipop;
} else{
currentapiVersion=R.mipmap.ic_launcher;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)......
I am using notifications in my app. Each notification has its own specific icon-image. On my phone the notifications work just fine.
However, when I receive the notification on my wearable it always shows the ic_launcher, and not the specific icon-image for the notification.
For testing purposes I am trying to have the wearable show icon_wearable.png in the notification. I already tried putting the icon_wearable.png in different drawable folders (xhdpi hdpi etc.). Right now it is located in the drawable-hdpi folder.
Code used for setting notifications:
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(someString);
builder.setContentText(message);
builder.setWhen(time.getTimeInMillis());
builder.setSmallIcon(R.drawable.some_image);
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
builder.extend(new Notification.WearableExtender()
.setContentIcon(R.drawable.icon_wearable));
}
Can someone please tell me why the wearable is always showing the ic_launcher and not icon_wearable?
Thanks in advance!
First make sure you use v4 support library because it allows you to create notifications using the latest notification features such as action buttons and large icons.
Make sure you add this line to your build.gradle file.
compile "com.android.support:support-v4:20.0.+"
And this necessary classes from the support library:
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
For the guideline and sample code on how to use Notification on Wearables, check this link.
For more information, you are correct to place the icon on the drawable-hdpi directory. Because it is stated in the linked documentation that:
Place other non-bitmap resources for wearable notifications, such as
those used with the setContentIcon() method, in the res/drawable-hdpi directory.
When using Parse for push notifications our app always displayed the application's launcher icon.
In the latest Android 5.1 version, the icon appears to be blank (a white square).
I tried setting the icon in the meta data:
<meta-data android:name="com.parse.push.notification_icon" android:resource="#drawable/noti_icon"/>
Based on the question here
But nothing seems to work.
Any ideas?
You must use a transparent and white icon under Android Lollipop 5.0 or greater. You can extend ParsePushBroadcastReceiver class and override the two methods to get your notification icon compatible with these Android APIs.
#Override
protected int getSmallIconId(Context context, Intent intent) {
return R.drawable.your_notifiation_icon;
}
#Override
protected Bitmap getLargeIcon(Context context, Intent intent) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
}
Remember to customize your code to support Lollipop and previous APIs.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon_lollipop);
}
else{
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
}
It is not related to Parse or push nitification, but just how Android 5.0 handles notification icons.
See this releated question for details:
Notification bar icon turns white in Android 5 Lollipop
Although #Pelanes has the correct answer (and should be accepted), here's what I did. Note that the Parse docs for getSmallIconId state the following:
Retrieves the small icon to be used in a Notification. The default implementation uses the icon specified by com.parse.push.notification_icon meta-data in your AndroidManifest.xml with a fallback to the launcher icon for this package. To conform to Android style guides, it is highly recommended that developers specify an explicit push icon.
So it is not entirely necessary to override the getSmallIconId() and getLargeIcon() methods.
What I did to solve the problem was I just made a copy of my icon, punched transparent "holes" into the icon, and set the com.parse.push.notification_icon meta-data in my manifest to point to this new icon.
For Android 5.0, it is required for your notification icon to be white and transparent, as others have mentioned. So creating the separate icon is necessary. One line in the manifest and one new drawable file is all it takes.
Try this code.
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(largeIcon)
.setContentText(data)
.setContentTitle("Notification from Parse")
.setContentIntent(pendingIntent);
I'm seeing the following exception in crash logs:
android.app.RemoteServiceException: Bad notification posted from package com.my.package: Couldn't create icon: StatusBarIcon(pkg=com.my.package user=0 id=0x7f02015d level=0 visible=true num=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1456)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
I'm posting my Notification from an IntentService from a PendingIntent set via the AlarmManager using the following method. All values passed in here are from the bundle extras in the PendingIntent / IntentService.
/**
* Notification
*
* #param c
* #param intent
* #param notificationId
* #param title
* #param message
* #param largeIcon
* #param smallIcon
*/
public static void showNotification(Context c, Intent intent,
int notificationId, String title, String message, int largeIcon,
int smallIcon) {
PendingIntent detailsIntent = PendingIntent.getActivity(c,
notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// BUILD
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
c);
// TITLE
mNotifyBuilder.setContentTitle(title).setContentText(message);
// ICONS
mNotifyBuilder.setSmallIcon(smallIcon);
if (Util.isAndroidOSAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
Bitmap large_icon_bmp = ((BitmapDrawable) c.getResources()
.getDrawable(largeIcon)).getBitmap();
mNotifyBuilder.setLargeIcon(large_icon_bmp);
}
mNotifyBuilder.setContentIntent(detailsIntent);
mNotifyBuilder.setVibrate(new long[] { 500, 1500 });
mNotifyBuilder.setTicker(message);
mNotifyBuilder.setContentText(message);
// NOTIFY
NotificationManager nm = (NotificationManager) c
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(notificationId, mNotifyBuilder.build());
}
From what I've seen of other answers - the exception I'm seeing happens when setSmallIcon() is not called properly.
I've checked and double checked that the Resource IDs being passed are all correct.
What was happening was, I was including the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager.
In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer that used to reference the correct drawable now referenced either the incorrect drawable or none at all (none at all - causing this crash)
Using VectorXml inside your notification has been known to cause this issue. Use png's
Don't use SVG on Kitkat!
I had the same issue every time when I wanted to show a notification on Kitkat. What caused the problem for me is that I have defined every icon in xml (from svg), the small icon and the action icon also. After I have replaced them with png-s the problem solved at my side.
My problem was that the icon I was using on
.setSmallIcon(R.drawable.ic_stat_push_notif)
wasn't generated accordingly. According to the official doc:
As described in Providing Density-Specific Icon Sets and Supporting
Multiple Screens, you should create separate icons for all generalized
screen densities, including low-, medium-, high-, and
extra-high-density screens. This ensures that your icons will display
properly across the range of devices on which your application can be
installed.
So the best way to fullfill the above, I used Notification Generator provided by Roman Nurik on https://romannurik.github.io/AndroidAssetStudio/index.html
In that way, you can use an image (taking into consideration that this has to have transparent background) and let the generator do the job for you generating the different sizes for notification icons.
The most important thing is that if the icon generator after you browse the image you are going to use shows you a white filled circle or square, there are problems with your image, maybe because it doesn't have any transparencies, so make sure that you has this ok.
android.app.RemoteServiceException: Bad notification posted
I had the same issue, but I was resolved. My problem is ".xml file" of Remote view.
In my xml file I was added one View in between the LinearLayout for divider.
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/view"
android:background="#000000" />
The above View component creating the Bad notification exception. This Exception reason is only xml file of Remoteviews.
After removing that View component, My code executed properly, without any exception. So I felt that Notification drawer not accepting any customized views.
So you don't draw any thing like the above view in the .xml file of RemoteView object.
In my app, this kind of bug happens only during upgrading. If the resource id changes in the newer version, Android RemoteView may fail to find the resource and throw out the RemoteServiceException. If you publish a 3rd version and do not change the resource id, the bugs may disappear only temporarily.
It is possible to reduce this kind of bugs by editing res/values/public.xml and res/values/ids.xml. Compiler will generate an individual resource id if the resource id is not in public.xml or ids.xml. When u change the resource name or add some new resources, the id may change and some devices may fail to find it.
So the step is as following:
Decompile the apk file and in res/values find the public.xml and ids.xml
Find all resources related to RemoteView in your app and copy them ( strings, dimen, drawable, layout, id, color... )
Create public.xml and ids.xml under res/values in your source code and paste the lines u just copied
Note:
Gradle 1.3.0 and above ignore the local public.xml. To make it work, u need to add some script in your build.gradle
afterEvaluate {
for (variant in android.applicationVariants) {
def scope = variant.getVariantData().getScope()
String mergeTaskName = scope.getMergeResourcesTask().name
def mergeTask = tasks.getByName(mergeTaskName)
mergeTask.doLast {
copy {
int i=0
from(android.sourceSets.main.res.srcDirs) {
include 'values/public.xml'
rename 'public.xml', (i == 0? "public.xml": "public_${i}.xml")
i++
}
into(mergeTask.outputDir)
}
}
}
}
Note:
This script does not support submodules project. I am trying to fix it.
In Android Studio version 3.0.0 and above, when adding a new image in the drawables folder choose drawable instead of drawable-v24.
If the image,you are using, is alread a (v24) just copy it and paste it in its same directory (eg. drawables). This time it will ask you which regular or v24 - just make sure its not the v24 and try it again this should fix the error.
Just in case the icon is not important, you can replace,
R.drawable.your_icon
To
android.R.drawable.some_standard_icon
This works!
I had RemoteServiceException when use Notification in my class extends from FirebaseMessagingService. I added the following code to AndroidManifest.xml:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_small" />
Also resource ic_small set in instance of a class Notification.Builder by method setSmallIcon(int icon).
You have pass same icon
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_name" />
and you notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
If you have a notification visible while your app is updated, you might run into this. What you may need to do is create a BroadcastReceiver that waits for a PACKAGE_CHANGED message, at which point you shutdown all your services which will also dismiss their associated notifications.
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED" />
</intent-filter>
</receiver>
Same issue happened with me as well and I have solved the same
REASON: This is happening because of we are using vector drawable for RemoteViews and vector drawable generates drawable at compile-time. Also I am not sure why some devices are not able to detect the generated drawable resources id while we are upping the app. But yes this was the reason.
REPRODUCE
Steps
1. Install previous build.
2. Send a notification
3. Update the build with next version code
4. After updating the app don't open the app and send notification again
SOLUTION
Replace the vector drawable with normal drawable(.png or .jpg) file.
I hope this resolves the problem.
You can put that vector xml file in both drawable and drawable v24. Those android version less than or equal to Android 6 will pick it up from drawable folder, and and newer version above android 6 will pick it up from drawable v24.
Its not about the Image type (vector, png, etc.) but the folder to which you are referring
to.
For me the error caused was due to a device (Nougat) referring to an icon present in v26 folder.
Solution: Just add the relevant icon to the other folder as well and the issue will be gone.
I had the same issue when I set a notification in a bundle.
I tried this and it solved my problem:
builder.setLargeIcon(large_icon);
builder.setSmallIcon(R.drawable.small_icon);
Make sure that setLargeIcon() invoked before setSmallIcon().
I too had the same issue. The issue is with the icon which you are using, I was using android.R.drawable.stat_sys_download. Go to drawables.xml and paste this.
<resources>
<item name="ic_start_download" type="drawable">#android:drawable/stat_sys_download</item>
</resource>
and then in your code
builder.setSmallIcon(R.drawable.ic_start_download);
You can try some other image instead of this image
My way to solve this: I had "bad" views in my layout (ex: a checkbox) - so I removed them.
RemoteViews seem to support only images and texts (to be confirmed by reading the doc).
I had RemoteServiceException when use android.support.constraint.ConstraintLayout. Change it to LinearLayout or Relative
and also android:layout_height="wrap_content" for container
(this is not solution for current question but helps someone with similar core issue) I too had the same issue, But in my case I have used corrupted .png file which is causing same issue. So I have deleted it and re-included correct .png file.
Please replace <android.support.v7.widget.AppCompatTextView with <TextView from custom notification layout.
Because android.support.v7.widget.AppCompatTextView or android.support.v7.widget.AppCompatImageView only works at runtime.
So Use TextView or ImageView
If any one still facing this issue :
Add a png file in your drawable folder with name ic_stat_ic_notification (or any name u like )
and in your manifest add below couple of lines
and u can create your icons over here - >
https://romannurik.github.io/AndroidAssetStudio/icon
I got this error due to autolink and linksClickable options in textview in notification view:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/app_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
Be careful!
It was a problem of icon location. In my case the notification icon was in drawable-anydpi-v24, I simply copied the icon in drawable folder and the error has gone.
Got this error in my Flutter android app and checked to see that all the resources are present and named correctly but still not working, I was using resources from mipmap. I changed it to drawable and placed the notification icons inside drawble folders and fixed the issue, hoping it might help someone later on
Synchrinize the project and than Clean it,
File > Synchronize then : Build > Clean Project
I hope that will help you all, It's work for me
This worked for me. Comment the reference of the icon and thus be able to use the NotificationCompat.Builder without problems.
$msg_notificacion = [
'title' => "titulonotif",
// "icon" : "myicon",
'body' =>"cuerponotif"
];