Uri默认声音通知?

Ste*_*fMa 122 notifications android uri

我使用Notification.Builder来构建通知.现在我想使用默认声音通知:

builder.setSound(Uri sound)
Run Code Online (Sandbox Code Playgroud)

但是Uri在默认通知中的位置是什么?

ρяσ*_*я K 261

尝试使用RingtoneManager获取默认通知Uri为:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

builder.setSound(uri);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.但我已经看到我也可以使用setDefaults(Notification.DEFAULT_SOUND);-) (49认同)
  • 你也可以传递这个`Settings.System.DEFAULT_NOTIFICATION_URI` (8认同)

For*_*ega 45

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 也有效

  • 设置来自import android.provider.Settings; (5认同)

Jor*_*sys 27

使用的两个选项Default Notification Sound是:

mBuilder.setDefaults(Notification.DEFAULT_SOUND);
Run Code Online (Sandbox Code Playgroud)

或使用RingtoneManager类:

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Run Code Online (Sandbox Code Playgroud)


Cri*_*vez 11

所有这些方法都有效

  1. mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

  2. mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

Google文档


sss*_*ock 5

对于系统默认通知

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Run Code Online (Sandbox Code Playgroud)

对于自定义通知

Uri customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.twirl);
Run Code Online (Sandbox Code Playgroud)

通知声音的来源(我重命名为“twirl”并放置在 res->raw 文件夹中)

https://notificationsounds.com/message-tones/twirl-470

通知生成器:

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notificaion_icon)
                        .setContentTitle("Title here")
                        .setContentText("Body here")
                        .setSound(defaultSoundUri)
                        .setAutoCancel(true);



NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(id, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)