I know there has be a lot of post about the issue. I have tried them all. Here are the steps I did.
First I found out that once a channel has been created it can't be change. The only way was to reinstall the app. So that's what I did and it did not work.
Second, some say I can delete the channel so I also did that using this code
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
//mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
and then recreating the channel after deletion.
Third, I tried using a new notification channel but I kept getting an error every time I use the new channel.
Here is the code I am using with all those solution that I tried
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
val mChannel = NotificationChannel(CHANNEL_ID, appName, NotificationManager.IMPORTANCE_HIGH)
mChannel.setShowBadge(false)
mChannel.setSound(notifSound, audioAttributes)
val mChannelnew = NotificationChannel(CHANNEL_ID2, appName, NotificationManager.IMPORTANCE_DEFAULT)
mChannelnew.setShowBadge(false)
mChannelnew.setSound(notifSound, audioAttributes)
mNotificationManager.createNotificationChannel(mChannel)
What am I missing? Any ideas? Thanks
Update: here is the code for notifsound
val notifSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.unconvinced)
Try these steps: Go to Settings > Sound & Notification > App Notifications. Select the app, and make sure that Notifications are turned on and set to Normal. Make sure that Do Not Disturb is turned off.
Silent: Your phone won't make a sound or vibrate. But the notification will show up when you swipe down from the top of your screen.
First I do not know Your Notification is not working on which devices like Oreo, Pie or below than N.
For your question StackOver Flow have lots of answer.
Now As per your question Your missing only one line of code But here unable to check your whole notification code because of you are not pasted yet.
Here I am Pasting a Notification code which is just fulfill your all notification requirement. (Full Custom Notification)
Notification With Image
public void createNotificationWithImage(String title,String message ,Bitmap image) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Custom Sound Uri
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
// Pay attention on below line here (NOTE)
mBuilder.setSound(soundUri);
if (image!=null) {
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setLargeIcon(image)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image).setSummaryText(message).bigLargeIcon(null))
.setColor(Color.GREEN)
.setContentIntent(resultPendingIntent);
}
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Now I am pasting code of notification which will work above or on OREO devices.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
below middle braces use for close your method.
}
Notification for without Image
public void createNotification(String title,String message){
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
R.mipmap.icon));
mBuilder.setSound(soundUri);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// notificationChannel.s
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
NOTE: In my code I mentioned that pay attention on one specific line where I descrided about to set sound Uri with Notification. You can describe like this.
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(soundUri)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
but it will not play a sound for your because after Oreo device is not set a sound as a priority level.
So always for sound use code as I described.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With