use of org.infobip.mobile.messaging.UserData in project mobile-messaging-sdk-android by infobip.
the class MainActivity method readMsisdnFromMobileMessaging.
private void readMsisdnFromMobileMessaging() {
UserData userData = MobileMessaging.getInstance(this).getUserData();
if (userData == null) {
return;
}
PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().putString(ApplicationPreferences.MSISDN, "" + userData.getMsisdn()).apply();
}
use of org.infobip.mobile.messaging.UserData in project mobile-messaging-sdk-android by infobip.
the class UserDataReporter method sync.
public void sync(final MobileMessaging.ResultListener listener, final UserData userData) {
if (userData == null) {
return;
}
mobileMessagingCore.saveUnreportedUserData(userData);
new MRetryableTask<UserData, UserData>() {
@Override
public UserData run(UserData[] userData) {
if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
MobileMessagingLogger.w("Can't report system data without valid registration");
throw InternalSdkError.NO_VALID_REGISTRATION.getException();
}
UserDataReport request = UserDataMapper.toUserDataReport(userData[0].getPredefinedUserData(), userData[0].getCustomUserData());
MobileMessagingLogger.v("USER DATA >>>", request);
UserDataReport response = mobileApiData.reportUserData(userData[0].getExternalUserId(), request);
MobileMessagingLogger.v("USER DATA <<<", response);
return UserDataMapper.fromUserDataReport(userData[0].getExternalUserId(), response.getPredefinedUserData(), response.getCustomUserData());
}
@Override
public void after(UserData userData) {
mobileMessagingCore.setUserDataReported(userData);
broadcaster.userDataReported(userData);
if (listener != null) {
listener.onResult(userData);
}
}
@Override
public void error(Throwable error) {
MobileMessagingLogger.e("MobileMessaging API returned error (user data)! ", error);
mobileMessagingCore.setLastHttpException(error);
stats.reportError(MobileMessagingStatsError.USER_DATA_SYNC_ERROR);
if (error instanceof BackendBaseExceptionWithContent) {
BackendBaseExceptionWithContent errorWithContent = (BackendBaseExceptionWithContent) error;
mobileMessagingCore.setUserDataReported(errorWithContent.getContent(UserData.class));
if (listener != null) {
listener.onError(MobileMessagingError.createFrom(error));
}
} else if (error instanceof BackendInvalidParameterException) {
mobileMessagingCore.setUserDataReportedWithError();
if (listener != null) {
listener.onError(MobileMessagingError.createFrom(error));
}
} else {
MobileMessagingLogger.v("User data synchronization will be postponed to a later time due to communication error");
if (listener != null) {
listener.onResult(UserData.merge(mobileMessagingCore.getUserData(), userData));
}
}
broadcaster.error(MobileMessagingError.createFrom(error));
}
}.retryWith(retryPolicy(listener)).execute(executor, userData);
}
use of org.infobip.mobile.messaging.UserData in project mobile-messaging-sdk-android by infobip.
the class AndroidBroadcasterTest method test_should_send_user_data.
@Test
public void test_should_send_user_data() {
// Given
UserData userData = new UserData();
userData.setFirstName("FirstName");
userData.setLastName("LastName");
// When
broadcastSender.userDataReported(userData);
// Then
Mockito.verify(contextMock, Mockito.times(1)).sendBroadcast(intentArgumentCaptor.capture());
Intent intent = intentArgumentCaptor.getValue();
assertEquals(Event.USER_DATA_REPORTED.getKey(), intent.getAction());
UserData userDataAfter = UserData.createFrom(intent.getExtras());
assertNotSame(userData, userDataAfter);
assertEquals("FirstName", userDataAfter.getFirstName());
assertEquals("LastName", userDataAfter.getLastName());
}
use of org.infobip.mobile.messaging.UserData in project mobile-messaging-sdk-android by infobip.
the class MainActivity method onMSISDNPreferenceChanged.
private void onMSISDNPreferenceChanged(SharedPreferences sharedPreferences) {
Long msisdn = null;
try {
if (sharedPreferences.contains(ApplicationPreferences.MSISDN)) {
msisdn = Long.parseLong(sharedPreferences.getString(ApplicationPreferences.MSISDN, "0"));
if (msisdn <= 0) {
throw new IllegalArgumentException();
}
}
} catch (Exception e) {
showToast(R.string.toast_message_userdata_invalid);
return;
}
if (msisdn != null) {
UserData userData = new UserData();
userData.setMsisdn(msisdn.toString());
MobileMessaging.getInstance(this).syncUserData(userData, new MobileMessaging.ResultListener<UserData>() {
@Override
public void onResult(UserData result) {
Log.v(TAG, "User data sync complete: " + result);
}
@Override
public void onError(MobileMessagingError e) {
Log.e(TAG, String.format(Locale.getDefault(), "User data sync error message: %s, code: %s" + e.getMessage(), e.getCode()));
}
});
}
}
Aggregations