Uri默认声音通知吗?


Answers:


267

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

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

builder.setSound(uri);

50
谢谢。但是我已经看到我也可以使用setDefaults(Notification.DEFAULT_SOUND);-)
StefMa 2012年

8
您也可以通过这个Settings.System.DEFAULT_NOTIFICATION_URI
Pratik Butani 2014年


29

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

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

或使用RingtoneManager类:

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


3

您也可以使用:

Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).
            getString("pref_tone", "content://settings/system/notification_sound"));
mBuilder.setSound(uri);

我想获取application声道声音,而不是默认的系统声音,请尝试使用 NotificationChannel channel = new NotificationChannel(ApplicationClass.getInstance().HighNotificationChannelID, getString(R.string.incoming_sms), NotificationManager.IMPORTANCE_HIGH); channel.getSound();返回功能, default system sound请帮忙
Sagar

3

对于系统默认通知

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

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

例如,如果您想禁用振动变化,则振动会变为新的long [] {0,0,0,0,0}; 您可以用声音或使用if else语句执行几乎类似的操作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.