Custom notification sound , android Oreo?

前端 未结 3 577
庸人自扰
庸人自扰 2020-12-09 18:12

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         


        
3条回答
  •  时光取名叫无心
    2020-12-09 18:47

    Simple answer:

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

提交回复
热议问题