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();
Related
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;
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());
I have a small problem with my Android app. It received notifications through FCM and shows them as push notification. So far it is all working, but the strange problem is, sometimes the icon is white and sometimes it is colourful.
When the app is open on the screen and I receive a push notification in this moment, the colourful push notification is shown on top of the screen.
When the app is closed, I get a push notification with a white icon.
I have attached a screenhot:
Screenshot
Here is the code snippet, where the push notification is created:
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.setColor(Color.parseColor("#83c3ed"))
.setLights(Color.RED, 1000, 500)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("WetterApp");
inboxStyle.addLine(notification.getTitle());
inboxStyle.addLine(notification.getBody());
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
My mobile device has Android 6.0.1, my SDK-version is 23.
Thanks for your help.
Well, I think the problem was something different. My code for creation the notification is only called, when the app is open on screen. When I receive a notification and the app is closed, the notification is handled by the Android system automaticly.
I had to set the color in the notification, which is send to the FCM Server:
$data = [
'notification' => [
'title' => 'Warnung: Wohnzimmer',
'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C',
'color' => '#83c3ed',
'sound' => 'default'
],
'to' => '/topics/testNotification'
];
Now I get a lightblue background icon within the app and also when the app is closed.
Follow the guide of google to create your icons, it's probably that it is occurring in the status bar too.
Then, try this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker(context.getResources().getString(R.string.app_name));
builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo);
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(
context.getResources().getString(R.string.app_name));
builder.setContentText(message);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setColor(ContextCompat.getColor(context, R.color.color_primary));
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, builder.build());
I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android Lollipop.
For instance:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My notification")
.setContentText("Look, white in Lollipop, else color!")
.setSmallIcon(getNotificationIcon())
.build();
return notification;
And, in the getNotificationIcon method:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}
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
}
I've just updated my ic_launcher in my android application (made in Android Studio, btw). I'm trying to create a test notification, and it used to work, but now it doesn't and I don't know why.
Notification.Builder builder = new Notification.Builder(this);
builder
.setWhen(System.currentTimeMillis())
.setContentTitle("TEST")
.setContentText("Hello! This is a notification test!")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NoMa.notify(1, builder.build());
It doesn't even work if I remove the setLargeIcon line, which in my point of view makes no sense. I don't discard the possibility that there's something wrong with my code. Thanks.
You have to setSmallIcon() if you want to show any notification.
So change your code to
Notification.Builder builder = new Notification.Builder(this);
builder
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher) //Any icon you want
.setContentTitle("TEST")
.setContentText("Hello! This is a notification test!")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NoMa.notify(1, builder.build());