Как сделать так, чтобы уведомление продолжало воспроизводить звук и вибрацию, даже если строка состояния опущена до android? - PullRequest
0 голосов
/ 28 апреля 2020
Уведомление типа

My NotificationCompat.CATEGORY_CALL отключается и прекращает вибрацию, когда строка состояния опускается. Я также попробовал это в forgroundservices, но то же самое поведение. Он перестает воспроизводить рингтон и вибрацию всякий раз, когда я опускаю строку состояния. Как я могу продолжать воспроизводить звук и вибрацию, пока не будет нажата кнопка действия? Я пробовал код ниже.

private static final String CHANNEL_ID = "myChannel" + System.currentTimeMillis();

private void showVideoCallNotification(Contacts c, String sdp) {
        Intent lockScreenAction = new Intent(this, IncomingCallActivity.class);
        lockScreenAction.putExtra("isCaller", false);
        lockScreenAction.putExtra("isCallReceived", true);
        lockScreenAction.putExtra("otherUserName", c.getName());
        lockScreenAction.putExtra("otherUser", c.getNumber());
        lockScreenAction.putExtra("sdp", sdp);
        lockScreenAction.setAction("LOCK_SCREEN");

        Intent receiveCallAction = new Intent(this, HeadsUpNotificationActionReceiver.class);
        receiveCallAction.putExtra("isCaller", false);
        receiveCallAction.putExtra("isCallReceived", true);
        receiveCallAction.putExtra("otherUserName", c.getName());
        receiveCallAction.putExtra("otherUser", c.getNumber());
        receiveCallAction.putExtra("sdp", sdp);
        receiveCallAction.setAction("RECEIVE_CALL");

        Intent cancelCallAction = new Intent(this, HeadsUpNotificationActionReceiver.class);
        cancelCallAction.putExtra("isCaller", false);
        cancelCallAction.putExtra("isCallReceived", false);
        cancelCallAction.putExtra("otherUserName", c.getName());
        cancelCallAction.putExtra("otherUser", c.getNumber());
        cancelCallAction.putExtra("sdp", sdp);
        cancelCallAction.setAction("CANCEL_CALL");

        RemoteViews notificationView = new RemoteViews(getPackageName(),
                R.layout.video_notification);
        notificationView.setTextViewText(R.id.tv_name, "test");

        PendingIntent lockScreenPendingIntent = PendingIntent.getActivity(this, 1202, lockScreenAction, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelCallPendingIntent = PendingIntent.getBroadcast(this, 1201, cancelCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(this, 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationView.setOnClickPendingIntent(R.id.bt_cancel, cancelCallPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.bt_receive, receiveCallPendingIntent);

        NotificationCompat.Builder notificationBuilder = null;
        Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
        notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_CALL)
                .setTimeoutAfter(30000)
                .setCustomHeadsUpContentView(notificationView)
                .setVibrate(new long[]{1000, 1000, 1000, 1000})
                .setAutoCancel(true)
                .setOngoing(true)
                .setSound(defaultRingtoneUri)
                .setFullScreenIntent(lockScreenPendingIntent, true);

        Notification incomingCallNotification = incomingCallNotification = notificationBuilder.build();
        incomingCallNotification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_INSISTENT;

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        createChannel(notificationManager);
        notificationManager.notify(1, incomingCallNotification);

    }

public void createChannel(NotificationManager notificationManager) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
            Ringtone defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "TEST", NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("Call Notifications");
            channel.enableVibration(true);
            channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
            channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000});
            channel.setSound(defaultRingtoneUri, defaultRingtone.getAudioAttributes());
            notificationManager.createNotificationChannel(channel);
        }
    }

enter image description hereenter image description here

...