Search in sources :

Example 1 with MRetryableTask

use of org.infobip.mobile.messaging.mobile.common.MRetryableTask in project mobile-messaging-sdk-android by infobip.

the class RegistrationSynchronizer method updateStatus.

public void updateStatus(final Boolean enabled) {
    new MRetryableTask<Boolean, Registration>() {

        @Override
        public Registration run(Boolean[] params) {
            String cloudToken = mobileMessagingCore.getCloudToken();
            Boolean pushRegistrationEnabled = params.length > 0 ? params[0] : null;
            MobileMessagingLogger.v("REGISTRATION >>>", cloudToken, pushRegistrationEnabled);
            RegistrationResponse registrationResponse = mobileApiRegistration.upsert(cloudToken, pushRegistrationEnabled);
            MobileMessagingLogger.v("REGISTRATION <<<", registrationResponse);
            return new Registration(cloudToken, registrationResponse.getDeviceApplicationInstanceId(), registrationResponse.getPushRegistrationEnabled());
        }

        @Override
        public void after(Registration registration) {
            setPushRegistrationEnabled(registration.enabled);
            setPushRegistrationId(registration.registrationId);
            setRegistrationIdReported(true);
            broadcaster.registrationEnabled(registration.cloudToken, registration.registrationId, registration.enabled);
        }

        @Override
        public void error(Throwable error) {
            MobileMessagingLogger.e("MobileMessaging API returned error (push registration status update)!");
            mobileMessagingCore.setLastHttpException(error);
            stats.reportError(MobileMessagingStatsError.PUSH_REGISTRATION_STATUS_UPDATE_ERROR);
            broadcaster.error(MobileMessagingError.createFrom(error));
        }
    }.retryWith(retryPolicyProvider.ONE_RETRY()).execute(executor, enabled);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) MobileApiRegistration(org.infobip.mobile.messaging.api.registration.MobileApiRegistration) RegistrationResponse(org.infobip.mobile.messaging.api.registration.RegistrationResponse)

Example 2 with MRetryableTask

use of org.infobip.mobile.messaging.mobile.common.MRetryableTask in project mobile-messaging-sdk-android by infobip.

the class VersionChecker method sync.

public void sync() {
    if (!SoftwareInformation.isDebuggableApplicationBuild(context)) {
        return;
    }
    Long lastCheckTime = PreferenceHelper.findLong(context, MobileMessagingProperty.VERSION_CHECK_LAST_TIME);
    Integer minimumInterval = PreferenceHelper.findInt(context, MobileMessagingProperty.VERSION_CHECK_INTERVAL_DAYS);
    if (TimeUnit.MILLISECONDS.toDays(Time.now() - lastCheckTime) < minimumInterval) {
        return;
    }
    new MRetryableTask<Void, VersionCheckResult>() {

        @Override
        public VersionCheckResult run(Void[] voids) {
            MobileMessagingLogger.v("VERSION >>>");
            LatestReleaseResponse response = mobileApiVersion.getLatestRelease();
            MobileMessagingLogger.v("VERSION <<<", response);
            return new VersionCheckResult(response);
        }

        @Override
        public void after(VersionCheckResult versionCheckResult) {
            if (versionCheckResult.hasError()) {
                MobileMessagingLogger.e("MobileMessaging API returned error (version check)!");
                stats.reportError(MobileMessagingStatsError.VERSION_CHECK_ERROR);
                return;
            }
            String current = SoftwareInformation.getSDKVersion();
            if (shouldUpdate(versionCheckResult.getVersion(), current)) {
                MobileMessagingLogger.w(TAG, "Your library version is outdated, find latest release " + versionCheckResult.getVersion() + " here: " + versionCheckResult.getUpdateUrl());
            }
            PreferenceHelper.saveLong(context, MobileMessagingProperty.VERSION_CHECK_LAST_TIME, Time.now());
        }

        @Override
        public void error(Throwable error) {
            mobileMessagingCore.setLastHttpException(error);
            MobileMessagingLogger.e("Error while checking version!");
            stats.reportError(MobileMessagingStatsError.VERSION_CHECK_ERROR);
        }
    }.retryWith(retryPolicyProvider.ONE_RETRY()).execute();
}
Also used : LatestReleaseResponse(org.infobip.mobile.messaging.api.version.LatestReleaseResponse) MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask)

Example 3 with MRetryableTask

use of org.infobip.mobile.messaging.mobile.common.MRetryableTask in project mobile-messaging-sdk-android by infobip.

the class SystemDataReporter method synchronize.

public void synchronize() {
    SystemData systemData = mobileMessagingCore.getUnreportedSystemData();
    if (systemData == null) {
        return;
    }
    new MRetryableTask<SystemData, SystemData>() {

        @Override
        public SystemData run(SystemData[] systemDatas) {
            if (systemDatas.length < 1 || systemDatas[0] == null) {
                MobileMessagingLogger.e(InternalSdkError.ERROR_EMPTY_SYSTEM_DATA.get());
                throw InternalSdkError.ERROR_EMPTY_SYSTEM_DATA.getException();
            }
            if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
                MobileMessagingLogger.w("Can't report system data without valid registration");
                throw InternalSdkError.NO_VALID_REGISTRATION.getException();
            }
            SystemData data = systemDatas[0];
            SystemDataReport report = from(data);
            MobileMessagingLogger.v("SYSTEM DATA >>>", report);
            mobileApiData.reportSystemData(report);
            MobileMessagingLogger.v("SYSTEM DATA <<<");
            return data;
        }

        @Override
        public void after(SystemData systemData) {
            mobileMessagingCore.setSystemDataReported();
            broadcaster.systemDataReported(systemData);
        }

        @Override
        public void error(Throwable error) {
            MobileMessagingLogger.w("Error reporting system data: " + error);
            mobileMessagingCore.setLastHttpException(error);
            stats.reportError(MobileMessagingStatsError.SYSTEM_DATA_REPORT_ERROR);
            if (!(error instanceof InternalSdkError.InternalSdkException)) {
                broadcaster.error(MobileMessagingError.createFrom(error));
            }
        }
    }.retryWith(policy).execute(executor, systemData);
}
Also used : SystemDataReport(org.infobip.mobile.messaging.api.data.SystemDataReport) MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) SystemData(org.infobip.mobile.messaging.SystemData)

Example 4 with MRetryableTask

use of org.infobip.mobile.messaging.mobile.common.MRetryableTask 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 5 with MRetryableTask

use of org.infobip.mobile.messaging.mobile.common.MRetryableTask in project mobile-messaging-sdk-android by infobip.

the class MessagesSynchronizer method sync.

public void sync() {
    if (!mobileMessagingCore.isPushRegistrationEnabled()) {
        return;
    }
    final String[] unreportedMessageIds = mobileMessagingCore.getAndRemoveUnreportedMessageIds();
    new MRetryableTask<Void, List<Message>>() {

        @Override
        public List<Message> run(Void[] objects) {
            if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
                MobileMessagingLogger.w("Can't sync messages without valid registration");
                throw InternalSdkError.NO_VALID_REGISTRATION.getException();
            }
            String[] messageIds = mobileMessagingCore.getSyncMessagesIds();
            SyncMessagesBody syncMessagesBody = SyncMessagesBody.make(messageIds, unreportedMessageIds);
            MobileMessagingLogger.v("SYNC MESSAGES >>>", syncMessagesBody);
            SyncMessagesResponse syncMessagesResponse = mobileApiMessages.sync(syncMessagesBody);
            MobileMessagingLogger.v("SYNC MESSAGES <<<", syncMessagesResponse);
            return MessagesMapper.mapResponseToMessages(syncMessagesResponse.getPayloads());
        }

        @Override
        public void after(List<Message> messages) {
            broadcaster.deliveryReported(unreportedMessageIds);
            if (messages == null || messages.isEmpty()) {
                return;
            }
            for (Message message : messages) {
                mobileMessageHandler.handleMessage(message);
            }
        }

        @Override
        public void error(Throwable error) {
            mobileMessagingCore.addUnreportedMessageIds(unreportedMessageIds);
            mobileMessagingCore.setLastHttpException(error);
            MobileMessagingLogger.e("MobileMessaging API returned error (synchronizing messages)! ", error);
            stats.reportError(MobileMessagingStatsError.SYNC_MESSAGES_ERROR);
            if (!(error instanceof InternalSdkError.InternalSdkException)) {
                broadcaster.error(MobileMessagingError.createFrom(error));
            }
        }
    }.retryWith(retryPolicy).execute(executor);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) SyncMessagesResponse(org.infobip.mobile.messaging.api.messages.SyncMessagesResponse) Message(org.infobip.mobile.messaging.Message) SyncMessagesBody(org.infobip.mobile.messaging.api.messages.SyncMessagesBody) List(java.util.List)

Aggregations

MRetryableTask (org.infobip.mobile.messaging.mobile.common.MRetryableTask)6 MobileApiRegistration (org.infobip.mobile.messaging.api.registration.MobileApiRegistration)2 RegistrationResponse (org.infobip.mobile.messaging.api.registration.RegistrationResponse)2 List (java.util.List)1 Message (org.infobip.mobile.messaging.Message)1 SystemData (org.infobip.mobile.messaging.SystemData)1 UserData (org.infobip.mobile.messaging.UserData)1 SystemDataReport (org.infobip.mobile.messaging.api.data.SystemDataReport)1 UserDataReport (org.infobip.mobile.messaging.api.data.UserDataReport)1 SyncMessagesBody (org.infobip.mobile.messaging.api.messages.SyncMessagesBody)1 SyncMessagesResponse (org.infobip.mobile.messaging.api.messages.SyncMessagesResponse)1 LatestReleaseResponse (org.infobip.mobile.messaging.api.version.LatestReleaseResponse)1 BackendBaseExceptionWithContent (org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)1 BackendInvalidParameterException (org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException)1