【问题标题】:NotificationChannel notification as alarm when phone is muted (Oreo)NotificationChannel 在手机静音时通知警报(奥利奥)
【发布时间】:2018-06-10 15:09:11
【问题描述】:

我将 NotificationCompat.Builder 设置为:

.setSound(getNotificationSound(), AudioManager.STREAM_ALARM)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_MAX)

在其他强制性属性中。

对于我正在使用的 NotificationChannel,我补充说:

.setBypassDnd(true)

对于奥利奥来说,问题在于:

  • 当手机静音或振动时,触发的通知没有任何声音,因此它不会像闹钟一样发挥作用

通知类别/频道中的请勿打扰自定义异常切换按钮有什么意义?因为我没有看到任何差异,它是否有助于实现我的目标?

对于比 Oreo 更早的版本,我没有使用 NotificationChannel,我有一个我更喜欢的行为:

  • 当手机静音但没有振动时,通知声音有效
  • 通知声音和振动在手机处于振动状态或启用声音时起作用

任何想法如何解决这种不一致?

【问题讨论】:

    标签: android notifications android-notifications android-alarms notification-channel


    【解决方案1】:

    最后我放弃了使用声音和振动的通知渠道来获得跨 Android 版本的一致结果。

    channel.setSound(null, null);
    

    并使用MediaPlayerVibrator 来代替像这样的辅助类:

    public class RingtoneAndVibrationPlayer extends ContextWrapper{
    
    private MediaPlayer mMediaPlayer;
    private Vibrator mVibrator;
    
    public RingtoneAndVibrationPlayer(Context context) {
        super(context);
    }
    
    public void play() {
        try {
            mMediaPlayer = new MediaPlayer();
            mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
    
            final Uri uri = Uri.parse(PreferenceHelper.getNotificationSound();
    
            mMediaPlayer.setDataSource(this, uri);
            if (PreferenceHelper.isRingtoneEnabled()) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mMediaPlayer.setLooping(PreferenceHelper.isRingtoneInsistent());
                mMediaPlayer.prepareAsync();
            }
    
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
    
            if (PreferenceHelper.isVibrationEnabled()) {
                mVibrator.vibrate(new long[] {0, 500, 500, 500},
                        PreferenceHelper.isRingtoneInsistent() ? 2 : -1);
            }
        } catch (SecurityException | IOException e) {
            stop();
        }
    }
    
    public void stop() {
        if (mMediaPlayer != null && mVibrator != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        if (mVibrator != null) {
            mVibrator.cancel();
        }
    }
    }
    

    我看到的唯一缺点是用户可以手动更改通知频道的声音和振动设置,该频道将与上述频道一起播放。 就我而言,在应用设置中明确偏好声音和振动是不鼓励这样做的。

    【讨论】:

    • 我想知道,就通知上下文而言,您何时开始创建RingtoneAndVibrationPlayer,以及如何存储对RingtoneAndVibrationPlayer 的引用?因为,在某些地方,我们需要引用RingtoneAndVibrationPlayer,以便我们可以调用stop()?谢谢。
    • 在我的用例中,我的服务中有一个 RingtoneAndVibrationPlayer 成员,在 onCreate 中初始化然后使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多