NotificationCompat setSound(sound, STREAM_ALARM) 不工作

nil*_*lsi 5 audio android android-notifications android-permissions

我正在构建一个 ping 功能,用于通过蓝牙查找丢失的手机。我需要手机发出声音,即使它被设置为静音/静音,就像闹钟通常的工作方式一样。我以为我可以把streamtype我的notificationAudioManager.STREAM_ALARM但它不工作。只有当电话声音打开时才会响起。我是这样设置的:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
        .setContentTitle("Ping")
        .setContentText("Device is trying to find your phone.")
        .setAutoCancel(false)
        .setSound(sound, STREAM_ALARM)
        .setVibrate(vibratePattern)
        .addAction(cancelAction);
Run Code Online (Sandbox Code Playgroud)

如果我尝试:

    Notification notification = builder.build();
    notification.audioStreamType = AudioManager.STREAM_ALARM;
Run Code Online (Sandbox Code Playgroud)

我从 Android Studio 收到一条警告,说 audioStreamType 已被弃用。是这种情况吗?notificaiton即使打开静音模式,还有其他方法可以发出声音吗?(最好也振动)

我通过为此目的创建专用媒体播放器来使其工作,但我认为不需要这样做。无论如何,我是这样做的:

    MediaPlayer mediaPlayer = new MediaPlayer();
    final String packageName = getApplicationContext().getPackageName();
    Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);

    try {
        mediaPlayer.setDataSource(this, sound);
    } catch (IOException e) {
        e.printStackTrace();
    }

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(false);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    } 
Run Code Online (Sandbox Code Playgroud)

bre*_*on2 1

使用builder.setSound(alarmSound, AudioManager.STREAM_AUDIO)正是我保持闹钟运转所需的东西!也许您的问题与R.raw.ping_sound您正在使用的声音样本有关。在尝试了一堆糟糕的实现之后,我在网上找到了警报通知(这就是我找到的地方Settings.System.DEFAULT_RINGTONE_URI),我遵循官方通知文档,然后使用NotificationCompat.Builder文档进行定制。

这是我的工作警报通知:

private void showNotification(){
    // Setup Intent for when Notification clicked
    Intent intent = new Intent(mContext, MedsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    // Setup Ringtone & Vibrate
    Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
    long[] vibratePattern = { 0, 100, 200, 300 };

    // Setup Notification
    String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
            .setContentText(notificationMessage)
            .setContentTitle(notificationTitle)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound, AudioManager.STREAM_ALARM)
            .setOnlyAlertOnce(true)
            .setVibrate(vibratePattern)
            .setAutoCancel(true);

    // Send Notification
    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)