自定义通知声音,Android Oreo吗? [英] Custom notification sound , android Oreo?

查看:82
本文介绍了自定义通知声音,Android Oreo吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序中的原始mp3或wav文件设置自定义通知声音. 下面是我的代码

I want to set a custom notification sound from a raw mp3 or wav file in my app. Below is my code

private void sendMyNotification(String message) {
    Intent intent;
    if (sharedPreferences.getBoolean(SPConstants.IS_LOGGED_IN, false)) {
        intent = new Intent(this, ActivityNotification.class);
    } else {
        intent = new Intent(this, ActivitySplash.class);
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.panic);
    AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(0, notificationBuilder.build());
}

紧急音频文件位于res-> raw中. 我尝试同时使用mp3和wav格式的声音,但是设置通知声音似乎无济于事. 我目前正在Pixel 2 OS 8.1上进行测试.

The panic audio file resides in res->raw. I have tried to use both mp3 and wav formats of the sound but nothing seems to work to set the notification sound. I am currently testing on Pixel 2 OS 8.1.

任何建议可能是什么问题?

Any suggestions what could be the issue?

推荐答案

  • 测试了打击代码,并按预期与我合作.

    • Tested blow code and worked with me as expected.

      添加内容意图,并且对我来说仍然没有任何问题.

      Add Content intent and that still working without any issues with me.

      private void sendMyNotification(String message) {
      
      Intent intent = new Intent(this, SplashActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
      
      Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.correct_answer);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentTitle(getString(R.string.app_name))
              .setContentText(message)
              .setAutoCancel(true)
              .setSound(soundUri)
              .setContentIntent(pendingIntent);
      
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      
          if(soundUri != null){
              // Changing Default mode of notification
              notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
              // Creating an Audio Attribute
              AudioAttributes audioAttributes = new AudioAttributes.Builder()
                      .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                      .setUsage(AudioAttributes.USAGE_ALARM)
                      .build();
      
              // Creating Channel
              NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
              notificationChannel.setSound(soundUri,audioAttributes);
              mNotificationManager.createNotificationChannel(notificationChannel);
          }
      }
      mNotificationManager.notify(0, notificationBuilder.build());
      }
      

    • 更新

      • 您可能需要卸载应用程序才能更改声音设置,请查看以下 查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆