android - Notification.Builder.setSound 无效

标签 android push-notification

我正在尝试在收到推送通知时播放声音。代码的要点如下所示:

Resources r = getResources();
int resourceId = r.getIdentifier(soundName, "raw", context.getPackageName());
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);
mBuilder.setSound(soundUri);

但是,当收到通知时,只会播放默认的通知声音。

如果我用 MediaPlayer 播放声音,它播放得很好。所以我必须假定 URI 是正确的。

MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, resourceId);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();

但是,我更愿意通过通知实例本身来解决这个问题,而不是手动播放声音。

最佳答案

在代码的其他地方,我们使用了 mBuilder.setDefaults(DEFAULT_ALL),这意味着 DEFAULT_SOUND。注释的文档:

Use the default notification sound. This will ignore any given sound.

关于android - Notification.Builder.setSound 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25566215/

相关文章:

android - 寻找一种允许客户端查看 NativeScript 应用程序项目实时更新的方法

java - 在 InputStream 中搜索

iOS 8 - 通知在新的 Ad Hoc 版本中停止工作

iPhone 推送通知声音 ,'voice notification"

ios - 对于通知推送,真正的 iPhone 设备在 App 首次加载时不会显示 Approval Alert

ios7.1:推送通知角标(Badge)更新问题

java - 使用 GCM 发送推送消息时错误代码 "notRegistered"是什么意思?

android - flutter应用开发中的依赖问题

android - fragment 中的静态数组适配器。好的还是坏的做法?

android - Firebase pyfcm 发送带有图像的推送通知