Android notification icon is blank on some devices - android

I have been getting a blank/grey notification icon on some Android devices (Pixel 2 XL and Pixel 5 - both Android 11), but it shows up fine on other Android devices I tested on (Huawei P20 & P30 - both Android 10). Has anyone else come across this anomaly?
This is what I tried:
I have ensured all icon sizes follow the defined bitmap
densities using the Android Asset Studio.
I also added the the .setColorized() method to the
Notification.Builder object as it was previously missing in the
code, but that made no difference at all. I have included my
notification code below:
private fun sendNotification(title: String?, message: String?, intent: Intent) {
val pendingIntent = PendingIntent.getActivity(
this,
0 /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT
)
// With the default notification channel id the heads up notification wasn't displayed
val channelId = getString(R.string.heads_up_notification_channel_id)
val notificationBuilder =
NotificationCompat.Builder(this, channelId).setSmallIcon(R.mipmap.ic_stat_ic_notification)
.setAutoCancel(true)
.setColorized(true)
.setColor(ContextCompat.getColor(this, R.color.color_orange))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
if (title != null) {
notificationBuilder.setContentTitle(title)
}
if (message != null) {
notificationBuilder.setContentText(message).setStyle(
NotificationCompat.BigTextStyle()
.bigText(message)
)
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"Test",
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Random.nextInt()/* ID of notification */, notificationBuilder.build())
}

I have had a similar Issue before. In my case the problem was the icon that I tried to use. I used a non-PNG file for the icon of the notification. When I switch it to PNG format, the nofication worked normally for all devices I tested.
If you are not using Png format for the icon , you can try this way.
Also, the problem can be the size of your icon in android reference website it is stated that
Set the small icon to use in the notification layouts. Different classes of devices may return different sizes.
Also see this guiadeline for Keyline Shapes

I tested on android 9,10,11. It's working fine.
In android studio, select image asset -> click icon type -> click Notification icons like this notification icon creation in android studio or create vector asset.both worked.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scrolling)
sendNotification("Hello","thinking of refering friend?",intent);
}
private fun sendNotification(title: String?, message: String?, intent: Intent) {
val pendingIntent = PendingIntent.getActivity(
this,
0 /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT
)
// With the default notification channel id the heads up notification wasn't displayed
val channelId = getString(R.string.heads_up_notification_channel_id)
val notificationBuilder =
NotificationCompat.Builder(this, channelId).setSmallIcon(R.drawable.abcd)
.setAutoCancel(true)
.setColorized(true)
.setColor(ContextCompat.getColor(this, R.color.color_orange))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
if (title != null) {
notificationBuilder.setContentTitle(title)
}
if (message != null) {
notificationBuilder.setContentText(message).setStyle(
NotificationCompat.BigTextStyle()
.bigText(message)
)
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"Test",
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Random.nextInt()/* ID of notification */, notificationBuilder.build())
}
}
let me know .

Related

Custom notification does not show in android

I am trying to show a custom notification when I get a push notification from firebase, when the app is in background.
When the app is in background, the log messages START and END gets printed but does not show the notification on Android which I created as customNotification.
This is how I created a notification.
Could you please suggest what should be corrected please
override fun onMessageReceived(message: RemoteMessage) {
Log.d("FCMService", "onMessageReceived START")
//super.onMessageReceived(message)
// Get the layouts to use in the custom notification
val notificationLayout = RemoteViews(packageName, R.layout.plugin_requires_approval_notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.plugin_requires_approval_notification_large)
// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(this, "FcmService")
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build()
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, customNotification)
Log.d("FCMService", "onMessageReceived END")
}
Thanks in advance
R
EDIT
it is fixed now, the issue was with the CHANNEL_ID I provided.
for reference channelID should be the one used in the notificationChannel() function which looks like the bellow code, this is in the project App class.
private fun createNotificationChannels() {
// Do this only on API 26+ because the NotificationChannel
// class is new and not in the AndroidX library
if (SDK_INT >= O) {
val caInfoNotifChannel = NotificationChannel(CaInfoProcessingService.NOTIFICATION_CHANNEL_ID,
getString(R.string.ca_info_service_notif_channel_name),
IMPORTANCE_LOW).apply {
description = getString(R.string.ca_info_service_notif_channel_desc)
}
// Register the channel with the system
getSystemService<NotificationManager>()?.createNotificationChannel(caInfoNotifChannel)
}
}

Notification not triggered on Android Wear OS

I am playing around with notifications on Wear OS and testing on a Fossil Falster 3 with Android API 28.
Why is the following notification not being triggered, in an standalone app.
The code is pretty much right from the Google documentation.
button_in.setOnClickListener {
val notificationId = 1
// The channel ID of the notification.
val id = "my_channel_01"
// Build intent for notification content
val viewPendingIntent = Intent(this, MainActivity::class.java).let { viewIntent ->
PendingIntent.getActivity(this, 0, viewIntent, 0)
}
// Notification channel ID is ignored for Android 7.1.1
// (API level 25) and lower.
val notificationBuilder = NotificationCompat.Builder(this, id)
.setLocalOnly(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("TITLE")
.setContentText("TEXT")
.setContentIntent(viewPendingIntent)
NotificationManagerCompat.from(this).apply {
notify(notificationId, notificationBuilder.build())
}
Log.d(TAG, "button was pressed!")
}
I can see the "button was pressed!" text, but I am not getting any notifications.
Watch apps require a notification channel, unlike Android apps which do not work like this.
In your code, val notificationId = 1 refers to the notification channel ID.
You can construct a NotificationChannel and register it like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(1, name, importance) // 1 is the channel ID
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
Notice in the commented code, in val mChannel = ..., you can see that the first parameter value 1 refers to the channel ID, as specified in your code in the OP.
You can read more about notification channels here: https://developer.android.com/training/notify-user/channels

Notification .setContentText not displayed on Android 10

I have a simple expandable notification.
Normal Layout:
Title
Short Text (.setContentText)
Explanded:
Title
Long Text (.setStyle)
In Android 10 (API 29) this does not work anymore as only the long text is partially shown. Take a look at the litte arrows in the top right corner.
val builder = NotificationCompat.Builder(this, CHANNEL_TEST_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("This is the title")
.setContentText("This is the content, which is not displayed in Android 10.")
.setStyle(NotificationCompat.BigTextStyle().bigText("The only displayed text in Android 10. contentText missing."))
.setPriority(NotificationCompat.PRIORITY_HIGH) // by channel from Android 8
.setAutoCancel(true) // Android 8
with(NotificationManagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(101, builder.build())
}
private const val CHANNEL_TEST_ID = "TEST"
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Test"
val descriptionText = "Test"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_TEST_ID, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
Is this a bug or a feature?
Google has confirmed this is a bug: https://issuetracker.google.com/issues/141403558

Android heads-up notification not Working

Tried Searching With no Luck yet
i am testing on API 24
whats wrong with this code? the notification is showing correctly but is not a heads-up notification though i specified the priority to high and added vibration and sound correctly.
Please any help would be appreciated
Here is my code:
private fun sendNotification(title: String, message: String) {
val mNotificationCompatBuilder = mNotificationUtils.getNotificationBuilder(
title,
message,
false,
R.drawable.ic_logo,
NotificationUtils.ALERT_MESSAGES_ID
)
mNotificationCompatBuilder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
mNotificationUtils.openTopActivityOnClick(mNotificationCompatBuilder, FTApplication.applicationContext())
mNotificationUtils.setSoundAndVibrate(mNotificationCompatBuilder)
val mNotificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.notify((title+message).hashCode(), mNotificationCompatBuilder.build())
}
fun getNotificationBuilder(title: String, body: String, onGoing: Boolean, icon: Int, id: String): NotificationCompat.Builder {
val largeIcon = BitmapFactory.decodeResource(
context.resources,
R.drawable.ic_logo
)
return NotificationCompat.Builder(context, id)
.setSmallIcon(icon)
.setLargeIcon(largeIcon)
.setBadgeIconType(icon)
.setContentTitle(title)
.setContentText(body)
.setOngoing(onGoing)
.setAutoCancel(!onGoing)
.setWhen(System.currentTimeMillis())
}
fun setSoundAndVibrate(builder: NotificationCompat.Builder) {
#Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) builder.priority = NotificationManager.IMPORTANCE_HIGH
else builder.priority = Notification.PRIORITY_HIGH
builder.priority = NotificationCompat.PRIORITY_HIGH // heads-up test
val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
builder.setSound(alarmSound)
builder.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
builder.setDefaults(Notification.DEFAULT_ALL)
}
here is the channel im creating (which is not used since as mentioned i am testing now on API 24)
#RequiresApi(Build.VERSION_CODES.O)
private fun createAlertMessageChannel() {
if (getManager()!!.getNotificationChannel(ALERT_CHANNEL_NAME) != null) {
return
}
// create alert channel
val alertChannel = NotificationChannel(
ALERT_MESSAGES_ID,
ALERT_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
)
alertChannel.description = "For Alerting User of Events"
alertChannel.setShowBadge(false)
alertChannel.enableLights(true)
alertChannel.enableVibration(true)
//alertChannel.setSound(null, null)
// Sets the notification light color for notifications posted to this channel
alertChannel.lightColor = Color.GREEN
// Sets whether notifications posted to this channel appear on the lock screen or not
alertChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
getManager()!!.createNotificationChannel(alertChannel)
}

How to disable sound/vibration for notification updates on a channel (API 26+)?

I have an application that allows the user to interact with notifications. Here is a simple use case: when the user taps on "Action," the app does some processing and updates the notification to show progress and updates it again to show whether the action was successful or not.
Prior to 26, I was able to set the sound/vibration on individual notifications so once the user click on "Action", the transition to the progress state would not make a sound/vibrate (the behavior that I want) but with 26, it seems like those arguments are no longer respected and the sound/vibration settings are only respected on the channel level.
My initial notification is supposed to make a sound/vibrate but if I am updating an existing (i.e. changing to progress state) then it should not make a sound/vibrate. Is there a way to accomplish that on API 26 and above?
Here is the code for setting up the initial state:
private fun sendNotification() {
val builder = NotificationCompat.Builder(this, "channel_id")
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val intent = Intent(this, MyIntentService::class.java)
val pIntent = PendingIntent.getService(this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val action = NotificationCompat.Action.Builder(
R.drawable.ic_lock_open_white_24dp,
"Action",
pIntent
).build()
builder.setSmallIcon(R.drawable.ic_home_teal_600_24dp)
.setContentTitle("My Title")
.setContentText("My content text")
.setSound(defaultSoundUri)
.addAction(action)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelName = "My Channel"
val description = "Channel Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("channel_id", channelName, importance)
channel.description = description
notificationManager.createNotificationChannel(channel)
}
val manager = NotificationManagerCompat.from(this)
manager.notify(ID, builder.build())
}
And the transition to the progress state (using same id)
private fun updateNotification(notificationId: Int, title: String) {
//This should NOT make sound or vibrate but it does on 26
val builder = NotificationCompat.Builder(this, "channel_id");
builder
.setSmallIcon(R.drawable.ic_home_teal_600_24dp)
.setContentTitle(title)
.setProgress(0, 0, true)
.setContentText("Processing...")
val manager = NotificationManagerCompat.from(this)
manager.notify(notificationId, builder.build())
}
On all API levels, you can disable sound and vibration for notification updates by use setOnlyAlertOnce():
Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.
builder.setOnlyAlertOnce(true)
This will ensure that updates to an existing notification won't sound/vibrate.

Categories

Resources