Звук уведомлений не работает со второй попытки - Android Studio - PullRequest
0 голосов
/ 21 ноября 2019

Я разработал приложение для Android, в котором я использовал функцию видеоконференцсвязи opentok. Я получаю уведомление о видеозвонке через push-уведомление firebase. Но когда я устанавливаю приложение в первый раз, оно получает звук при уведомлении, затем, когда я принимаю и перехожу на экран видеоконференции другого действия, после этого при следующем уведомлении о вызове звучит только вибрация, а не звонок.

Ниже мое уведомлениефрагмент кода:

MyFirebaseMessagingServie.class

    private void sendCallNotification(int status, String message, int queryId) {
        saveNotificationLog(status, queryId);
        long[] vibrate = new long[]{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
        final UserManager userManager = new UserManager(getApplicationContext());
        boolean isLoggedIn = userManager.isLoggedIn().get();
        Intent notificationIntent = isLoggedIn ? new Intent(this, HomeDoctorView.class) : new Intent(this, LoginView.class);
        if (status == 6 || status == 7) {
            notificationIntent.putExtra(Params.INTENT.SHOULD_UDPATE, true);
        }
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri defaultSoundUri = Uri.parse("android.resource://com.myapplication.app/" + R.raw.iphone_ringtone);
        Utils.printLog("FireBaseMessagingService", defaultSoundUri.toString());
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CALL_CHANNEL)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message)
                .setSound(defaultSoundUri)
                .setVibrate(vibrate)
                .setAutoCancel(false)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setSmallIcon(R.drawable.ic_notification);
            notificationBuilder.setColor(ContextCompat.getColor(this, R.color.cerulean));
        } else {
            notificationBuilder.setSmallIcon(R.drawable.ic_notification);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager.getNotificationChannel(CALL_CHANNEL) == null) {
                notificationManager.createNotificationChannel(Utils.createNotifChannel(this, defaultSoundUri, CALL_CHANNEL));
            }
        }
        notificationManager.notify(notificationId, notificationBuilder.build());
    }

Utils.java

@RequiresApi(Build.VERSION_CODES.O)
    public static NotificationChannel createNotifChannel(Context context, Uri defaultSoundUri, String channelId) {
        String channelName = channelId.equals("call_channel") ? "Call Alerts" : "Notification Alerts";
        NotificationChannel channel = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_HIGH);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build();
        if (channelId.equals("call_channel")) {
            channel.setDescription("Alerts related to calls");
            channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000, 1000, 1000});
            channel.setBypassDnd(true);
            channel.setSound(defaultSoundUri, audioAttributes);
            channel.enableVibration(true);
            channel.enableLights(true);
            channel.setShowBadge(false);
        } else {
            channel.setDescription("Alerts related to prescription, scheduled appointments, etc.");
            channel.setVibrationPattern(new long[]{500, 500, 500, 500});
            channel.setBypassDnd(true);
            channel.setSound(defaultSoundUri, audioAttributes);
            channel.enableVibration(true);
            channel.enableLights(true);
            channel.setShowBadge(false);
        }
        NotificationManager manager = context.getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);
        Log.d("CHANNEL", "createNotifChannel: created " + channelId + "\n" + defaultSoundUri);
        return channel;
    }
...