Using custom drawables in an Android Notification - android

Is there any way to use an image that I'm generating on the fly as a Notification icon?

This topic was discussed in a previous SO question. Upshot: I don't think it is possible.
UPDATE: For tablets or other large-screen devices, on API Level 11+, there is a largeIcon property which accepts a Bitmap. AFAIK, this is unused on smaller-screen devices, such as phones.

You can now change the main icon with API 11 (Notification.Builder -> setLargeIcon)

Try creating a custom Notifications view, then you should be able to use a Bitmap object:
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewBitmap(R.id.icon_view, myBitmapObject);
notification.contentView = contentView;
For more info see "Creating a Custom Expanded View" on this page: https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Related

Android 5+ custom notification XML layout with RemoteViews, set correct icon tint for ImageButton

My app is using a custom Notification layout with RemoteViews.
To display text, the layout is using the following system styles:
android:TextAppearance.Material.Notification.Title
android:TextAppearance.Material.Notification
This works fine.
However, the TextAppearance style can't be used to set the value of android:tint, so I had to hardcode the color.
To my best knowledge, there's no special system style for setting notification ImageButton tint.
Hardcoded colors work fine on the current Android 5+ systems, but some users install custom ROMs with custom dark themes, and the notification looks wrong, i.e. black icons on black background.
Is there any way to get the system notification icon / imagebutton color, and apply it from an XML layout?
Or maybe there's another way to achieve this?
Sorry, But as per my knowledge custom ROM's have separate system designs,configurations and that are not official as well.
So,supporting Custom ROM without knowledge about its design is not possible.
And android APIs are for supporting official ROM's.
Hope it Helps!!
Try this example :
Definition for notification :
// Declare variable
public static Bitmap icon;
// Assign value but this line can be different depends on current
// android sdk vesion ...
icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.YOUR_IMAGE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setShowWhen(false);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setSmallIcon(R.drawable.image1); // One way to load img
mBuilder.setContentText("this text not visible");
mBuilder.setLargeIcon(icon);// This is the line
mBuilder.setPriority(Notification.PRIORITY_DEFAULT);
mBuilder.setContent(contentNotifySmall); // ORI
//mBuilder.setAutoCancel(false);
mBuilder.setCustomBigContentView(contentNotify);
I setup small and big variant for any case , this is important.
for background can you try these attributes...
app:backgroundTint="#color/pay"
---------Or-------------
android:tint="#color/white"
You can take textColor from TextAppearance_Compat_Notification_Title and add it as tint to an image programmatically like this
val remoteViews = RemoteViews(service.packageName, R.layout.notification_layout)
val icon = Icon.createWithResource(context, R.drawable.icon)
val attrs = intArrayOf(android.R.attr.textColor)
with(context.obtainStyledAttributes(R.style.TextAppearance_Compat_Notification_Title, attrs)) {
val iconColor = getColor(0, Color.GRAY)
icon.setTint(iconColor)
recycle()
}
remoteViews.setImageViewIcon(R.id.ibPlayPause, icon)
You can use a notificationlistenerservice to get an active notification. This returns a list of StatusBarNotifications, then just:
StatusBarNotifcation sBNotification = activeNotifications[0];
Notification notification = sBNotification.getNotification();
int argbColor = notification.color;
If you change ImageButton to ImageView in your layout you can update it with
RemoteViews remoteView = getRemoteViews(context);
// load base icon from res
Bitmap baseIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.base_icon);
// edit Bitmap baseIcon any way for your choose
Bitmap editedBitmap = ...
// update notification view with edited Bitmap
remoteView.setImageViewBitmap(R.id.button_icon, edited);
P.S. you can edit Bitmap like this:
https://stackoverflow.com/a/5935686/7630175 or any other way
Hope it's help

Android wearable not showing icon set by setcontenticon()

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.

Custom Big Notification In API level 10

I'm using a Custom View for notifications.
In this case, I know that for a big size notification, I have to use bigContentView and set a big view for it.
Every thing is good in API 16, But my app minimum SDK is API level 10 and bigContentView is not supported in API level 10.
myNotification= notification.setContentTitle("some string")
.setContentText("some text")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
RemoteViews notificationView = new RemoteViews(MyApp.getAppContext().getPackageName(),
R.layout.custom_notification_layout);
myNotification.bigContentView=notificationView; //Error in this line!
Is there another way to display big layout for a custom notification?
Or is a older version of bigContentView?
Big view for notifications were introduced in and android 4.1 and they're not supported on older devices. (as #Pop Tudor commented)
We can use it by bigContentView(RemteView) for notifications if api level is upper than 15.
In API 15 or lower, we can only set custom view for notifications by contentView(View) method.

Android 5.1 push notification icon is blank

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);

How to display timestamp in Android Notifications for Honeycomb+ devices?

I'd like to show a timestamp in an Android Notification, just like the Android Design Guidelines suggest. (see the first snapshot, "12:03PM" is what I want!).
I tried many different ways, I thought setWhen would do it, but it only seems to affect the ordering in the Notification tray. Any idea how to achieve that?
See my code below:
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notify)
.setLargeIcon(largeIcon)
.setTicker(text)
.setNumber(unreadCount)
.setWhen((new Date()).getTime())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_VIBRATE);
notificationManager.notify(NOTIFICATION_ID, builder.getNotification());
I don't want to use a custom layout for the notification.
I think I found the answer myself, when seems to be displayed only on Android 4.0.3 and later.
My wife's Nexus S (4.0.3) shows it, and I just upgraded to 4.0.4 on my Galaxy Nexus and it magically started to work!
It also shows on older versions (2.3 for instance), but not on 4.0.2 !
Use setContentInfo and pass your date as a string into it.
From this link
public Notification.Builder setContentInfo (CharSequence info) Since: API Level 11
Set the large text at the right-hand side of the notification.
Large Text at the right hand side of the notification should be where you want to put time.
Also from the same link:
public Notification.Builder setWhen (long when) Since: API Level 11
Set the time that the event occurred. Notifications in the panel are
sorted by this time.
which means that setWhen only sorts the notifications, and doesn't set any text.

Categories

Resources