【问题标题】:How to add custom notification sound in Android [duplicate]如何在Android中添加自定义通知声音[重复]
【发布时间】:2015-10-02 14:29:11
【问题描述】:

我必须管理通知,为此我需要管理自定义声音。那么您知道我们如何做到这一点吗?

我已经将声音文件复制到我的原始文件夹中,所以任何人都知道如何将它实施到我的项目中。

提前致谢。

【问题讨论】:

标签: android push-notification google-cloud-messaging


【解决方案1】:

首先将 Resource (res) 中的文件夹命名为 raw 并将文件 (YOUR_SOUND_FILE.MP3) 放入其中,然后使用下面的代码行自定义声音

   NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context,
            SlidingMenuActivity.class);
    notificationIntent.putExtra("isInbox", true);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

将这些代码行用于自定义声音

 notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play


    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

编辑更新

对于奥利奥及更高版本,您需要检查SDK_VERSION并使用NotificationChannelsetSound方法

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.FILE_NAME);  //Here is FILE_NAME is the name of file that you want to play

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
                "YOUR CHANNEL NAME",
                NotificationManager.IMPORTANCE_DEFAULT)

            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                    context.getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            mChannel.setDescription(msg);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(sound, attributes); // This is IMPORTANT


            if (mNotificationManager != null)
                mNotificationManager.createNotificationChannel(mChannel);
        }

【讨论】:

  • 7.0以上如何播放自定义通知声音?我尝试使用路径file://content://FILE PROVIDER 无法播放自定义声音,只有系统声音可以播放。
  • 等待您的回复。我试过的是developer.android.com/reference/android/os/…
  • @SagarHudge 请参考上面更新的解决方案,如果有问题请告诉我
  • @SagarHudge 从我的角度来看,你不能播放通知的外部文件..你需要放在原始文件夹中
  • @Ravindra Kushwaha,谢谢
【解决方案2】:
Intent i = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setContentTitle("I want food")
    .setContentText(notificationcontent)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pi)
    .setAutoCancel(true)
    .setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
mp.start();
manager.notify(73195, builder.build());

【讨论】:

  • 是的。这是有效的,但也添加了默认通知声音。如何阻止它?
  • 我也想知道这个问题的答案
  • @SsubratRrudra - 你现在可能已经想通了。但是对于其他人 setSound(null) 会做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多