126

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

builder.setSound(Uri sound)

但是默认通知的 Uri 在哪里?

4

7 回答 7

279

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

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

builder.setSound(uri);
于 2012-06-30T06:37:57.690 回答
46

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)也可以

于 2013-07-23T09:07:30.853 回答
29

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

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

或使用RingtoneManager类:

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
于 2014-07-04T22:11:00.590 回答
12

所有这些方法都有效

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

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

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

谷歌文档

于 2015-03-25T08:25:30.380 回答
5

对于系统默认通知

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

对于自定义通知

Uri customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.twirl);

通知声音的来源(我重命名为“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());
于 2017-08-11T11:18:50.123 回答
3

您也可以使用它:

Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).
            getString("pref_tone", "content://settings/system/notification_sound"));
mBuilder.setSound(uri);
于 2015-02-24T21:50:13.597 回答
0

如果有人仍然需要,这对声音和振动非常有效。

        Context context = getApplicationContext();
    long[] vibrate = new long[] { 1000, 1000, 1000, 1000, 1000 };
    Intent notificationIntent = new Intent(context, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Resources res = context.getResources();
    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.notif)
            .setTicker("lastWarning")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setVibrate(vibrate)
            //.setContentTitle(res.getString(R.string.notifytitle)) 
            .setContentTitle("Notification")
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            //.setContentText(res.getString(R.string.notifytext))
            .setContentText("Notification text"); 

    // Notification notification = builder.getNotification(); // until API 16
    Notification notification = builder.build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFY_ID, notification);

如果您想禁用例如振动,请将振动更改为 new long[] { 0,0,0,0,0}; 您可以使用声音或使用 if else 语句做几乎类似的事情。

于 2017-03-22T13:10:10.830 回答