use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.
the class FirebaseService method onTokenRefresh.
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// Save the Token in Preferences
Context context = getApplicationContext();
PreferencesManager prefManager = PreferencesManager.getInstance(context);
prefManager.setFirebaseDeviceID(refreshedToken);
XMPPUser xmppUser = prefManager.getXMPPUser();
if (xmppUser != null && xmppUser.getUsername() != null) {
String authToken = prefManager.getAuthorizationToken();
if (authToken != null) {
NotificationUtil.sendDeviceTokenToServer(refreshedToken, xmppUser.getUsername(), authToken, context);
}
}
}
use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.
the class XMPPService method createConnection.
private void createConnection() throws XmppStringprepException, NoSuchAlgorithmException {
if (mConnection == null) {
AndroidSmackInitializer androidSmackInitializer = new AndroidSmackInitializer();
androidSmackInitializer.initialize();
ExtensionsInitializer extensionsInitializer = new ExtensionsInitializer();
extensionsInitializer.initialize();
XMPPUser xmppUser = PreferencesManager.getInstance(XMPPService.this.getApplicationContext()).getXMPPUser();
XMPPTCPConnectionConfiguration connConfig = XMPPTCPConnectionConfiguration.builder().setXmppDomain(JidCreate.domainBareFrom(DOMAIN)).setHost(HOST).setPort(PORT).setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible).setCustomSSLContext(SSLContext.getInstance("TLS")).setSocketFactory(SSLSocketFactory.getDefault()).setUsernameAndPassword(xmppUser.getUsername(), xmppUser.getPassword()).build();
SmackConfiguration.setDefaultPacketReplyTimeout(5000);
XMPPTCPConnection.setUseStreamManagementDefault(true);
XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
mConnection = new XMPPTCPConnection(connConfig);
mConnection.setPacketReplyTimeout(5000);
mConnection.setPreferredResumptionTime(10);
mConnection.setUseStreamManagement(true);
mConnection.setUseStreamManagementResumption(true);
mConnection.addAsyncStanzaListener(packetListener, packetFilter);
mConnection.addAsyncStanzaListener(pingPacketListener, pingPacketFilter);
mConnection.addStanzaAcknowledgedListener(new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
// TODO Acknowledgement
ChatMessage chatMessage = ChatMessageDAO.getChatMessageByStanzaId(packet.getStanzaId());
if (chatMessage != null) {
chatMessage.setAcknowledged(true);
chatMessage.save();
EventBus.getDefault().post(new MessageAcknowledgementEvent(chatMessage, chatMessage.getAcknowledged()));
}
}
});
SASLAuthentication.unregisterSASLMechanism("org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism");
SASLAuthentication.registerSASLMechanism(new CustomSCRAMSHA1Mechanism());
mConnection.addConnectionListener(connectionListener);
ServerPingWithAlarmManager.getInstanceFor(mConnection).setEnabled(true);
// ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();
}
try {
if (!mConnection.isConnected() && !mConnection.isAuthenticated()) {
mConnection.connect();
} else if (mConnection.isConnected() && !mConnection.isAuthenticated()) {
mConnection.login();
}
} catch (Exception e) {
// e.printStackTrace();
}
}
use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.
the class XMPPService method createUser.
private void createUser(final String username) {
if (creatingUser) {
return;
}
try {
creatingUser = true;
String salt = "a04aa6a74e76bf8f57b0e2e715138171";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update((username + salt).getBytes());
byte[] byteData = md.digest();
// convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
String hash = sb.toString();
String url = "https://sso.botplatform.io/createUser";
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", username);
jsonObject.put("hash", hash);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success") && PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
JSONObject data = response.getJSONObject("data");
PreferencesManager.getInstance(getBaseContext()).setXMPPUser(new XMPPUser(data.getString("username"), data.getString("password")));
creatingUser = false;
XMPPService.this.login();
}
} catch (Exception e) {
e.printStackTrace();
creatingUser = false;
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
creatingUser = false;
}
});
jsonObjectRequest.setRetryPolicy(retryPolicy);
if (PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
queue.add(jsonObjectRequest);
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.
the class PreferencesManager method getXMPPUser.
public XMPPUser getXMPPUser() {
String username = pref.getString("xmpp-token", null);
String password = pref.getString("xmpp-key", null);
return username != null ? new XMPPUser(username, password) : null;
}
Aggregations