I am going nuts with a problem with notifications
on Android: While I was developing my project, suddenly the emulator plays no notification sounds anymore for API
26 and higher,
e.g. the API which require a channel.
Of course I have set up a channel and it has worked great before! I have reinstalled the app, deleted the channel, even set up another AVD with a API
27, same result: no sound ! (the notification does pop up)
Obviously I have checked that notification sounds are enabled, also for this specific channel, all seems OK, just no sounds.
If I play a test using:
RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
it works as it should, so no hardware problem.
On lower APIs
pre 26 where you don't need a channel, the sound does play.
Anybody had the same problem?
//make the channel
//The Config class is imported and the constants resolved, not the problem
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(
Config.CHANNEL_1_ID,
Config.CHANNEL_1_NAME,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(Config.CHANNEL_1_DESC);
channel.enableLights(true);
channel.enableVibration(true);
channel.setShowBadge(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
// send notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, Config.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(false)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat mNotificationMgr = NotificationManagerCompat.from(context);
mNotificationMgr.notify(1, mBuilder.build());
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…