代码之家  ›  专栏  ›  技术社区  ›  WISHY

自定义通知声音,安卓奥利奥?

  •  9
  • WISHY  · 技术社区  · 6 年前

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

    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中。 我目前正在Pixel2OS8.1上测试。

    有什么建议吗?有什么问题吗?

    2 回复  |  直到 6 年前
        1
  •  39
  •   Khaled Lela    6 年前
    • 测试了blow代码并按预期与我合作。

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

    更新

    • 您可能需要卸载应用程序来更改声音设置,请检查这些 link
        2
  •  3
  •   oxied    6 年前

    简单回答:

    Uri soundUri = Uri.parse(
                             "android.resource://" + 
                             getApplicationContext().getPackageName() +
                             "/" + 
                             R.raw.push_sound_file);
    
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();
    
    // Creating Channel
    NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
                                                          "YOUR_CHANNEL_NAME",
                                                          NotificationManager.IMPORTANCE_HIGH);
    channel.setSound(soundUri, audioAttributes);
    
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                                               .createNotificationChannel(notificationChannel);
    
        3
  •  0
  •   mecky roon    5 年前

    晚了,但可能对某些人有帮助,只需在你的通知兼容性生成器()实例:

    .setSound("your sound uri",AudioManager.STREAM_NOTIFICATION)
    

    注: 作为 NotificationCompat.Builder() 是向后兼容的,所以 AudioAttributes