自定义通知声音,Android Oreo吗?

时间:2018-06-25 18:17:19

标签: android uri android-notifications android-8.0-oreo

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

class MyApplicationState extends State<MyApplication>{
...
@override
  Widget build(BuildContext context) {
    return new FlatButton(
      child: new Text("Print Sample Text"),
      onPressed :(){
       // i want to cal the function here how is it possible to call the 
       // function 
       // printSample()  from here??  
      }
    );
  }
...
}

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

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

3 个答案:

答案 0 :(得分:17)

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

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

    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 了解更多详细信息。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

最新但对某些人可能会有所帮助,只需在NotificationCompat.Builder()实例的下面添加以下行即可:

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

注意: 由于NotificationCompat.Builder()是向后兼容的,因此不需要通知频道中的AudioAttributes

相关问题