gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:48 26 4
gpt4 key购买 nike

我想在我的应用程序中设置来自原始 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, 0, 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());
}

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

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

最佳答案

  • 测试了 blow 代码并按预期与我一起工作。

  • 添加内容 Intent ,我仍然可以正常工作。

    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 了解更多详情。

关于android - 自定义通知声音,安卓奥利奥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com