Search in sources :

Example 1 with UserData

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();
}
Also used : UserData(org.infobip.mobile.messaging.UserData)

Example 2 with UserData

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);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) BackendInvalidParameterException(org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException) UserData(org.infobip.mobile.messaging.UserData) UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport) BackendBaseExceptionWithContent(org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)

Example 3 with 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());
}
Also used : UserData(org.infobip.mobile.messaging.UserData) Intent(android.content.Intent) Test(org.junit.Test)

Example 4 with UserData

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()));
            }
        });
    }
}
Also used : MobileMessaging(org.infobip.mobile.messaging.MobileMessaging) UserData(org.infobip.mobile.messaging.UserData) MobileMessagingError(org.infobip.mobile.messaging.mobile.MobileMessagingError)

Aggregations

UserData (org.infobip.mobile.messaging.UserData)4 Intent (android.content.Intent)1 MobileMessaging (org.infobip.mobile.messaging.MobileMessaging)1 UserDataReport (org.infobip.mobile.messaging.api.data.UserDataReport)1 MobileMessagingError (org.infobip.mobile.messaging.mobile.MobileMessagingError)1 MRetryableTask (org.infobip.mobile.messaging.mobile.common.MRetryableTask)1 BackendBaseExceptionWithContent (org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)1 BackendInvalidParameterException (org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException)1 Test (org.junit.Test)1