自定义通知声音,android奥利奥?

前端之家收集整理的这篇文章主要介绍了自定义通知声音,android奥利奥?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的应用程序中从原始mp3或wav文件设置自定义通知声音.
以下是我的代码
  1. private void sendMyNotification(String message) {
  2. Intent intent;
  3. if (sharedPreferences.getBoolean(SPConstants.IS_LOGGED_IN,false)) {
  4. intent = new Intent(this,ActivityNotification.class);
  5. } else {
  6. intent = new Intent(this,ActivitySplash.class);
  7. }
  8. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  9. PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,PendingIntent.FLAG_ONE_SHOT);
  10. Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  11. soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.panic);
  12. AudioManager manager = (AudioManager)getSystemService(Context.AUdio_SERVICE);
  13. manager.setStreamVolume(AudioManager.STREAM_MUSIC,100,0);
  14. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
  15. .setSmallIcon(R.mipmap.ic_launcher)
  16. .setContentTitle(getString(R.string.app_name))
  17. .setContentText(message)
  18. .setAutoCancel(true)
  19. .setSound(soundUri)
  20. .setContentIntent(pendingIntent);
  21. NotificationManager notificationManager =
  22. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  23. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  24. NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,channelName,NotificationManager.IMPORTANCE_HIGH);
  25. notificationManager.createNotificationChannel(mChannel);
  26. }
  27. notificationManager.notify(0,notificationBuilder.build());
  28. }

恐慌音频文件位于res-> raw.
我试图使用声音的mp3和wav格式,但似乎没有什么工作来设置通知声音.
我目前正在测试Pixel 2 OS 8.1.

有什么建议可能是什么问题?

解决方法

>经过测试的打击代码并按预期与我合作.
>添加内容意图,仍然可以正常工作,没有任何问题.
  1. private void sendMyNotification(String message) {
  2.  
  3. Intent intent = new Intent(this,SplashActivity.class);
  4. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  5. PendingIntent pendingIntent = PendingIntent.getActivity(this,PendingIntent.FLAG_ONE_SHOT);
  6.  
  7. Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.correct_answer);
  8. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"CH_ID")
  9. .setSmallIcon(R.mipmap.ic_launcher)
  10. .setContentTitle(getString(R.string.app_name))
  11. .setContentText(message)
  12. .setAutoCancel(true)
  13. .setSound(soundUri)
  14. .setContentIntent(pendingIntent);
  15.  
  16. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  17. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  18.  
  19. if(soundUri != null){
  20. // Changing Default mode of notification
  21. notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
  22. // Creating an Audio Attribute
  23. AudioAttributes audioAttributes = new AudioAttributes.Builder()
  24. .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  25. .setUsage(AudioAttributes.USAGE_ALARM)
  26. .build();
  27.  
  28. // Creating Channel
  29. NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
  30. notificationChannel.setSound(soundUri,audioAttributes);
  31. mNotificationManager.createNotificationChannel(notificationChannel);
  32. }
  33. }
  34. mNotificationManager.notify(0,notificationBuilder.build());
  35. }

更新

>您可能需要卸载应用程序以更改声音设置,请查看这些link获取更多详细信息.

猜你在找的Android相关文章