optimise login and push notification code
@@ -67,16 +67,19 @@ public class Kommunicate {
private static final String CONVERSATION_ASSIGNEE = "CONVERSATION_ASSIGNEE";
private static final String SKIP_ROUTING = "SKIP_ROUTING";
public static final String KM_CHAT_CONTEXT = "KM_CHAT_CONTEXT";
+ public static final String KM_ALREADY_LOGGED_IN_STATUS = "ALREADY_LOGGED_IN";
public static void init(Context context, String applicationKey) {
Applozic.init(context, applicationKey);
}
- public static void login(Context context, KMUser kmUser, KMLoginHandler handler) {
+ public static void login(Context context, final KMUser kmUser, final KMLoginHandler handler) {
if (isLoggedIn(context)) {
+ String loggedInUserId = MobiComUserPreference.getInstance(context).getUserId();
+ if (loggedInUserId.equals(kmUser.getUserId())) {
RegistrationResponse registrationResponse = new RegistrationResponse();
- registrationResponse.setMessage("USER ALREADY LOGGED IN");
- Contact contact = new ContactDatabase(context).getContactById(MobiComUserPreference.getInstance(context).getUserId());
+ registrationResponse.setStatusMessage(KM_ALREADY_LOGGED_IN_STATUS);
+ Contact contact = new ContactDatabase(context).getContactById(loggedInUserId);
if (contact != null) {
registrationResponse.setUserId(contact.getUserId());
registrationResponse.setContactNumber(contact.getContactNumber());
@@ -86,6 +89,19 @@ public class Kommunicate {
registrationResponse.setStatusMessage(contact.getStatus());
}
handler.onSuccess(registrationResponse, context);
+ } else {
+ logout(context, new KMLogoutHandler() {
+ @Override
+ public void onSuccess(Context context) {
+ login(context, kmUser, handler, null);
+ }
+
+ @Override
+ public void onFailure(Exception exception) {
+ handler.onFailure(null, exception);
+ }
+ });
+ }
} else {
login(context, kmUser, handler, null);
}
@@ -108,6 +124,7 @@ public class Kommunicate {
@Override
public void onSuccess(Context context) {
KmDatabaseHelper.getInstance(context).deleteDatabase();
+ Kommunicate.setDeviceToken(context, null);
logoutHandler.onSuccess(context);
}
@@ -437,8 +454,16 @@ public class Kommunicate {
}
public static void registerForPushNotification(Context context, String token, KmPushNotificationHandler listener) {
+ if (TextUtils.isEmpty(token)) {
+ listener.onFailure(null, new KmException("Push token cannot be null or empty"));
+ return;
+ }
+ setDeviceToken(context, token);
+
+ if (!token.equals(getDeviceToken(context))) {
new PushNotificationTask(context, token, listener).execute();
}
+ }
public static void registerForPushNotification(Context context, KmPushNotificationHandler listener) {
registerForPushNotification(context, Kommunicate.getDeviceToken(context), listener);
