android - 模拟器上没有通知声音

标签 android notifications

Android 上的 notifications 问题让我抓狂:当我开发我的项目时,模拟器突然不再播放 API 26 及更高版本的通知声音,

例如需要 channel 的 API。 当然我已经建立了一个 channel 并且它之前非常有效!我重新安装了应用程序,删除了 channel ,甚至用 API 27 设置了另一个 AVD,结果相同:没有声音! (通知确实弹出)

显然,我已经检查过通知声音是否已启用,对于这个特定 channel ,一切似乎都正常,只是没有声音。 如果我使用以下方式进行测试:

RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();

它正常工作,所以没有硬件问题。 在不需要 channel 的较低 API pre 26 上,声音会播放。

有人遇到同样的问题吗?

//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());

最佳答案

似乎我找到了答案:我必须在模拟器上完成“完成设置您的 Android SDK”向导。单击“跳过”一切,现在它似乎再次工作。 奇怪的是,我最初并没有这样做,但通知仍然按预期工作......呃!

关于android - 模拟器上没有通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986070/

相关文章:

ios - 我正在使用推送套件并接收消息,但通知中心未显示该消息

java - 为什么我的程序不能正确显示 2016 年 2 月及之后的月份?

android - 我应该在 drawable-mdpi 文件夹中放置什么尺寸的图像

android - Azure禁用推送通知android

ios - 如何在iOS中定义推送通知的类型?

android - 为什么我的通知小图标总是灰色的?

Android异步服务调用策略

java - 从服务器(java)发送数据到android客户端

android - 如何在android中创建像Iphone Edittext框一样的渐变

javascript - 如何从 Chrome 创建桌面通知?