Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

android - No notification sound on the emulator

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems I found the answer: I had to go through the "Finish setting up your Android SDK" wizard on the emulator. Clicked "skip" for everything, now it seems to work again. Weirdly enough, I didn't do that initially and still the notifications worked as expected... duh !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...