Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了自定义通知声音,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 = @L_475_4@manager.getDefaultUri(@L_475_4@manager.TYPE_NOTIFICATION);
    soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackagename() + "/" + R.raw.panic);
    Au@L_121_6@manager manager = (Au@L_121_6@manager)getSystemservice(Context.AUdio_serviCE);
    manager.setStreamVolume(Au@L_121_6@manager.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奥利奥?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。