【问题标题】:Android Notification custom sound not workingAndroid通知自定义声音不起作用
【发布时间】:2016-03-03 10:30:49
【问题描述】:

我在网上搜索并找到了一些与在 android 通知上设置声音相关的帖子并关注了它们,但在我的情况下,我在 android 通知上获得了默认通知声音。这是我的代码:

builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_message));

我也关注了this 的帖子,但没有运气。

供您参考:我已经使用 MediaPlayer 播放了 URI,然后它工作正常。

我的部分代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext);
builder.setContentTitle(message.notificationTitle);
builder.setContentText(message.notificationSubtitle);
builder.setTicker(message.notificationTitle);
builder.setSmallIcon(R.drawable.ic_action_inbox);
Uri uri = Uri.parse("android.resource://" + appContext.getPackageName() + "/" + R.raw.notification_message);
builder.setSound(uri);
builder.setVibrate(new long[]{1000, 1000, 1000});
builder.build();

谁能解释一下是什么问题。怎么做?我被卡住了:(

【问题讨论】:

  • 检查你的日志看看是否有异常信息。

标签: android android-notifications


【解决方案1】:

对不起各位。这是我的错误。 我使用了以下行:

builder.setDefaults(Notification.DEFAULT_ALL);

这造成了问题。注释行修复它。

感谢您的回答:)

【讨论】:

    【解决方案2】:

    使用Context.getPackageName()方法获取Application的包名。

     Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.notification_message)
    

    删除

    Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_message)
    

    【讨论】:

    • 你认为我没有检查 getpackageName() 方法。它工作正常。
    • 我的 URI 是正确的。我已经使用 Mediaplayer 在 URI 中播放了声音。但是在设置 builder.setSound() 时它不起作用。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2020-07-17
    相关资源
    最近更新 更多