Реагировать на нативное приложение, используя сбой pjsip на Android после того, как какое-то время отошел от фона - PullRequest
0 голосов
/ 28 февраля 2019

Я занимаюсь разработкой мобильного приложения voip с реагированием на нативное использование react-native-pjsip.Он хорошо работает на ios, но вылетает при восстановлении из фона через некоторое время на android.

Я создал это BackgroundService и PusherReceiver в android, чтобы запустить react native activity при получении pushandroid для отображения звонка в фоновом режиме.

BackgroundService.java

public class BackgroundService extends IntentService {
    public static final String EXTRA_ISFROMPUSH = "com.fonality.hudmobile/com.voximplantdemo.isFromPush";

    public BackgroundService() {
        super("BackgroundService");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Intent i = new Intent(getBaseContext(), MainActivity.class);

        // is from push param
        i.putExtra(EXTRA_ISFROMPUSH, true);

        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (intent != null) {
            startActivity(i);

            PusherReceiver.completeWakefulIntent(intent);
        }
    }
}

PusherReceiver.java

public class PusherReceiver extends WakefulBroadcastReceiver {
    public void onReceive(final Context context, Intent intent) {
        if (!isAppOnForeground((context))) {
            String custom = intent.getStringExtra("custom");

            try {
                if (custom != null) {
                    JSONObject notificationData = new JSONObject(custom);
                }

                // This is the Intent to deliver to our service.
                Intent service = new Intent(context, BackgroundService.class);
                // Put here your data from the json as extra in in the intent

                // Start the service, keeping the device awake while it is launching.
                startWakefulService(context, service);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        final String packageName = context.getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
}

Спасибо

...