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

我想在我的应用程序中从原始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,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.

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

解决方法

>经过测试的打击代码并按预期与我合作.
>添加内容意图,仍然可以正常工作,没有任何问题.
private void sendMyNotification(String message) {

Intent intent = new Intent(this,SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,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获取更多详细信息.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...