QuickBlox / quickblox-android-sdk

QuickBlox Android SDK includes code snippets with main use cases and framework JAR library.
BSD 3-Clause "New" or "Revised" License
416 stars 697 forks source link

Unable to receive incoming call when app is closed. #568

Closed nicemak closed 6 years ago

nicemak commented 6 years ago

Help avoid duplicate issue reports, check [existing issues](link to issues section of repo)

Environment details (Operating system, browser information, SDK version) Android Oreo 8.1.0, SDK version: 3.7.0

Did this work before? No

Expected behavior When app is closed and someone calling me, it should display calling activity

Actual behavior a notification comes only

`public class CallService extends Service { private static final String TAG = CallService.class.getSimpleName(); // private QBChatService chatService; private QBRTCClient rtcClient; private PendingIntent pendingIntent; private int currentCommand; private QBUser currentUser;

public static void start(Context context, QBUser qbUser, PendingIntent pendingIntent) {
    Intent intent = new Intent(context, CallService.class);

    intent.putExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_LOGIN);
    intent.putExtra(Consts.EXTRA_QB_USER, qbUser);
    intent.putExtra(Consts.EXTRA_PENDING_INTENT, pendingIntent);

    context.startService(intent);
}

public static void start(Context context, QBUser qbUser) {
    start(context, qbUser, null);
}

public static void logout(Context context) {
    Intent intent = new Intent(context, CallService.class);
    intent.putExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_LOGOUT);
    context.startService(intent);
}

@Override
public void onCreate() {
    super.onCreate();

    createChatService();

    Log.d(TAG, "Service onCreate()");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Service started");

    parseIntentExtras(intent);

    startSuitableActions();

    return START_REDELIVER_INTENT;
}

private void parseIntentExtras(Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        currentCommand = intent.getIntExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_NOT_FOUND);
        pendingIntent = intent.getParcelableExtra(Consts.EXTRA_PENDING_INTENT);
        currentUser = (QBUser) intent.getSerializableExtra(Consts.EXTRA_QB_USER);
    }
}

private void startSuitableActions() {
    if (currentCommand == Consts.COMMAND_LOGIN) {
        startLoginToChat();
    } else if (currentCommand == Consts.COMMAND_LOGOUT) {
        logout();
    }
}

private void createChatService() {
    if (YouchatApplication.getInstance().qbChatService == null) {
        QBChatService.setDebugEnabled(false);
        YouchatApplication.getInstance().qbChatService = QBChatService.getInstance();
    }
}

private void startLoginToChat() {
    if (!YouchatApplication.getInstance().qbChatService.isLoggedIn()) {
        if (currentUser == null) {
            currentUser = new QBUser(
                    "User" + SecurePreferences.getStringPreference(getApplicationContext(),
                            SecurePreferences.USER_ID),
                    "Userpass" + SecurePreferences.getStringPreference(getApplicationContext(),
                            SecurePreferences.USER_ID));
            currentUser.setFullName(SecurePreferences.getStringPreference(getApplicationContext(), "name"));
            StringifyArrayList<String> strings = new StringifyArrayList<>();
            strings.add("" + SecurePreferences.getStringPreference(getApplicationContext(),
                    SecurePreferences.GET_CURRENT_PHONE));
            currentUser.setTags(strings);
        } else if (currentUser.getLogin() == null || currentUser.getLogin().length() == 0) {
            currentUser.setLogin("" + SecurePreferences.getStringPreference(getApplicationContext(),
                    SecurePreferences.GET_CURRENT_PHONE));
        } else if (currentUser.getPassword() == null || currentUser.getPassword().length() == 0) {
            currentUser.setPassword("" + SecurePreferences.getStringPreference(getApplicationContext(),
                    SecurePreferences.GET_CURRENT_PHONE));
        } else if (currentUser.getFullName() == null || currentUser.getFullName().length() == 0) {
            currentUser.setFullName(SecurePreferences.getStringPreference(getApplicationContext(), "name"));
        } else if (currentUser.getTags() == null || currentUser.getTags().size() == 0) {
            StringifyArrayList<String> strings = new StringifyArrayList<>();
            strings.add("" + SecurePreferences.getStringPreference(getApplicationContext(),
                    SecurePreferences.GET_CURRENT_PHONE));
            currentUser.setTags(strings);
        }
        loginToChat(currentUser);
    } else {
        sendResultToActivity(true, null);
    }
}

private void loginToChat(QBUser qbUser) {

    YouchatApplication.getInstance().qbChatService.login(qbUser, new QBEntityCallback<QBUser>() {
        @Override
        public void onSuccess(QBUser qbUser, Bundle bundle) {
            Log.d(TAG, "login onSuccess");
            startActionsOnSuccessLogin();
        }

        @Override
        public void onError(QBResponseException e) {
            Log.d(TAG, "login onError " + e.getMessage());
            sendResultToActivity(false, e.getMessage() != null
                    ? e.getMessage()
                    : "Login error");
        }
    });
}

private void startActionsOnSuccessLogin() {
    initPingListener();
    initQBRTCClient();
    sendResultToActivity(true, null);
}

private void initPingListener() {
    ChatPingAlarmManager.onCreate(this);
    ChatPingAlarmManager.getInstanceFor().addPingListener(new PingFailedListener() {
        @Override
        public void pingFailed() {
            Log.d(TAG, "Ping chat server failed");
        }
    });
}

private void initQBRTCClient() {
    rtcClient = QBRTCClient.getInstance(getApplicationContext());
    // Add signalling manager
    if (YouchatApplication.getInstance().qbChatService != null) {
        if (YouchatApplication.getInstance().qbChatService.getVideoChatWebRTCSignalingManager() != null) {
            YouchatApplication.getInstance().qbChatService.getVideoChatWebRTCSignalingManager().addSignalingManagerListener(new QBVideoChatSignalingManagerListener() {
                @Override
                public void signalingCreated(QBSignaling qbSignaling, boolean createdLocally) {
                    if (!createdLocally) {
                        rtcClient.addSignaling((QBWebRTCSignaling) qbSignaling);
                    }
                }
            });
        }
    }

    // Configure
    QBRTCConfig.setDebugEnabled(true);
    SettingsUtil.configRTCTimers(CallService.this);

    // Add service as callback to RTCClient
    rtcClient.addSessionCallbacksListener(WebRtcSessionManager.getInstance(this));
    rtcClient.prepareToProcessCalls();
}

private void sendResultToActivity(boolean isSuccess, String errorMessage) {
    if (pendingIntent != null) {
        Log.d(TAG, "sendResultToActivity()");
        try {
            Intent intent = new Intent();
            intent.putExtra(Consts.EXTRA_LOGIN_RESULT, isSuccess);
            intent.putExtra(Consts.EXTRA_LOGIN_ERROR_MESSAGE, errorMessage);

            pendingIntent.send(CallService.this, Consts.EXTRA_LOGIN_RESULT_CODE, intent);
        } catch (PendingIntent.CanceledException e) {
            String errorMessageSendingResult = e.getMessage();
            Log.d(TAG, errorMessageSendingResult != null
                   ? errorMessageSendingResult
                    : "Error sending result to activity");
        }
    }
}

private void logout() {
    destroyRtcClientAndChat();
}

private void destroyRtcClientAndChat() {
    if (rtcClient != null) {
        rtcClient.destroy();
    }
    ChatPingAlarmManager.onDestroy();
    if (YouchatApplication.getInstance().qbChatService != null) {
        YouchatApplication.getInstance().qbChatService.logout(new QBEntityCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid, Bundle bundle) {
                YouchatApplication.getInstance().qbChatService.destroy();
            }

            @Override
            public void onError(QBResponseException e) {
                Log.d(TAG, "logout onError " + e.getMessage());
                YouchatApplication.getInstance().qbChatService.destroy();
            }
        });
    }
    stopSelf();
}

@Override
public void onDestroy() {
    Log.d(TAG, "Service onDestroy()");
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "Service onBind)");
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.d(TAG, "Service onTaskRemoved()");
    super.onTaskRemoved(rootIntent);
    destroyRtcClientAndChat();
}

}`

RomanPronin commented 6 years ago

So, if you got notification about call, by this event you can start CallService - CallService.start(), which can launch calling activity or whatever you need. Just have a look at sample video chat more carefully.

nicemak commented 6 years ago

the problem is i can't find where notification received, i completely removed FCM but i receive notifications, if i know the place where notification arrives i will start the CallService

RomanPronin commented 6 years ago

To listen push notification you need to add QBFcmPushListenerService and register it in manifest, have a look here in sample. Also find useful information by this reference.

RomanPronin commented 6 years ago

Long time inactivity.