Android colorful notification pictures - android

I want to implement colorful, customized notification in my Android App.
I tried to implement color, but for the moment I can only set the background color. I want to reach this kind of result:
With a image of my app shown as a notification pic when expanded.
Here my code at the moment:
return builder.setContentTitle("APP TITLE")
.setContentText("Some texts")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_ALARM)
.setOnlyAlertOnce(false)
.setColor(getResources().getColor(R.color.colorPrimary));
How can I implement the icon notification colorful, as the hangouts example I posted above?

mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messs)
.setLargeIcon(bitmap)
.setSmallIcon(R.mipmap.hangouts_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
mNotifyBuilder.setContentIntent(pIntent);
// Set Vibrate, Sound and Light           
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText(messs);
mNotificationManager.notify(m, mNotifyBuilder.build());

Related

Android O: disable heads-up

My application could play some audio.
Once user press at the audio title the notification with the play/pause/rewind buttons appears at the notification drawer.
I noticed that in Android O (8) this notification appears with "heads-up" effect.
I don't need heads-up notification, I need just silent common notification. How can I disable "heads-up" effect?
NotificationCompat.Builder notificationBuilder;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID);
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
}
Notification notification = notificationBuilder
.setSmallIcon(R.drawable.tray_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setWhen(System.currentTimeMillis())
.setContent(remoteViews)
.setChannelId(Constants.CHANNEL_ID)
.build();
notification.defaults = 0;

Updated notification with custom layout changes to default layout android

I have a custom layout notification which populates some data (distance data).
I have to update the distance in the existing notification.
When I update it , my custom layout changes to default android notification layout.
New notification creation code.
remoteViews = notificationCustomUI(poi_name, formatDistanceText(poi_type, distance), left_action, right_action, pendingLeftIntent, pendingRightIntent);
notificationBuilder = new NotificationCompat.Builder(this); // creating new builder for new notification.
notification = notificationBuilder.setSmallIcon(R.mipmap.launcher)
.setContentText(formatDistanceText(poi_type, distance))
.setContentTitle(poi_name)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingLeftIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDeleteIntent(getDeleteIntent(notificationId, poi_id, poi_type))
.build();
notification.bigContentView = remoteViews;
notificationManager.notify(notificationId, notification);
Updating exiting notification code
remoteViews.setTextViewText(R.id.tv_distance, formatDistanceText(poi_type, distance));
notificationManager.notify(existingNotificationId, notificationBuilder.build());

setgroup() in notification not working

I'm tring to create notification group, this is my code:
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("New mail from " + 1)
.setContentText("cv")
.setSmallIcon(R.drawable.rh_logo)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faaborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("johndoe#gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
// Issue the notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
When I run the app, and send notification messages, they do not show in group.
Can someone explain me what I need to change?
You have to create a group notification before creating your custom notification. Just like this:
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setGroupSummary(true)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent);
Do not forget setGroupSummary to true.
Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
.setContentTitle(title)
.setContentText(content)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent)
Finally use NoticationManagerCompat to notify them.
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(GROUP_ID, groupBuilder.build());
manager.notify(id, builder.build());
Replace
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
with
NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);
because you are using NotificationCompat
There is a strange behavior of notification. If you add autocancel notification group doesn't work, if you add setGroup or setGroupSummary. It will not work. Removing all that fixed the problem for me. Basically, you don't need to mention any group it will auto group. TESTED on Redmi 10 pr0, Poco x3 NFC, huawei device.
return new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_LOW);

Android disable notification sounds

I want to create a notification without any sounds. How can I do this?
I tried the code below but it's not working for me:
notification = mBuilder
.setStyle(notiStyle)
.setSmallIcon(notificationIcon)
.setTicker(title)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(null).build();
You can create the NotificationCompat.Builder object without using setSound(). This will create a notification without any sound.
notification = mBuilder
.setStyle(notiStyle)
.setSmallIcon(notificationIcon)
.setTicker(title)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.build();
After your notification builder, add
notification.defaults = 0;
to tell the notification manager not to make any default values when not specified (eg. you set to null so it takes the default value, adding this will remove this behavior and so disable sounds completely).
In Android 26+ (Oreo) you do this by setting notification channel importance level to 'IMPORTANCE_LOW' or below.
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}

Notification icon is round white instead of application icon in lollipop

I have generated a local notification by this following code.
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("message")
.setWhen(System.currentTimeMillis())
.setSmallIcon(getNotificationIcon())
.build();
private static int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_loli : R.drawable.ic_launcher;
}
where icon icon_loli is 16*16 white icon. still for the api version below 21, it works very fine but on lollipop & above it will show a notification as in image below
This is because of setSmallIcon(), and with the material design it is only black and white because the main purpose is to show uniformed icon into the top bar. If you want your logo, you will have to use setLargeIcon() like:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("message")
.setWhen(System.currentTimeMillis())
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();

Categories

Resources