amadeu01
10/23/2017 - 5:03 PM

Service to send SMS

Service to send SMS


import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import com.klinker.android.send_message.ApnUtils;
import com.klinker.android.send_message.Message;
import com.klinker.android.send_message.Settings;
import com.klinker.android.send_message.Transaction;

import java.util.Date;

import br.com.agileprocess.agilealerts.App;
import br.com.agileprocess.agilealerts.data.Alert;
import br.com.agileprocess.agilealerts.data.AlertDatabase;
import br.com.agileprocess.agilealerts.receivers.SmsDeliveredReceiver;
import br.com.agileprocess.agilealerts.receivers.SmsSentReceiver;
import timber.log.Timber;


public class SMSMMSService extends IntentService {

    public static final String ACTION_SEND_SMS = "br.com.agileprocess.services.extra.ACTION_SEND_SMS";
    public static final String ACTION_SEND_MMS = "br.com.agileprocess.services.extra.ACTION_SEND_MMS";
    public static final String MESSAGE = "br.com.agileprocess.services.extra.MESSAGE";
    public static final String PHONE_NUMBER = "br.com.agileprocess.services.extra.PHONE_NUMBER";
    public static final String ALERT_REMOTE_ID = "br.com.agileprocess.services.extra.ALERT_REMOTE_ID";



    public SMSMMSService() {
        super("SMSMMSService");
    }

    public SMSMMSService(String name) {
        super(name);
    }

    public static void sendSMS(Context context,
                               String message,
                               String phoneNumber,
                               Integer alertRemoteId) {
        Intent intent = new Intent(context, SMSMMSService.class);
        intent.setAction(ACTION_SEND_SMS);
        intent.putExtra(MESSAGE, message);
        intent.putExtra(PHONE_NUMBER, phoneNumber);
        intent.putExtra(ALERT_REMOTE_ID, alertRemoteId);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            if (intent != null) {
                final String action = intent.getAction();
                if (ACTION_SEND_SMS.equals(action)) {
                    String message = intent.getStringExtra(MESSAGE);
                    String phoneNumber = intent.getStringExtra(PHONE_NUMBER);
                    Integer alertRemoteId = intent.getIntExtra(ALERT_REMOTE_ID, -1);
                    sendMessage(message, phoneNumber, alertRemoteId);
                }
            }
        } catch (Exception exception) {
            Timber.e(exception);
            App.logException(exception);
        }
    }

    private void initSettings() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            initApns();
        }
    }

    private void initApns() {
        ApnUtils.initDefaultApns(this, new ApnUtils.OnApnFinishedListener() {
            @Override
            public void onFinished() {
            }
        });
    }

    private Transaction initSmsTransaction() {
        initSettings();
        Settings sendSettings = new Settings();
        sendSettings.setUseSystemSending(true);
        sendSettings.setDeliveryReports(true);

        Transaction transaction = new Transaction(this, sendSettings);

        transaction.setExplicitBroadcastForDeliveredSms(
                SmsDeliveredReceiver.getIntent(this)
        );
        transaction.setExplicitBroadcastForSentSms(
                SmsSentReceiver.getIntent(this)
        );

        return transaction;
    }

    private void sendSMS(String textMessage, String phoneNumber){
        Transaction transaction = initSmsTransaction();
        Message message = new Message(textMessage, phoneNumber);
        transaction.sendNewMessage(message, Transaction.NO_THREAD_ID);
    }

    public void sendMessage(String textMessage, String phoneNumber, Integer alertRemoteId) {
        try {
            Answers.getInstance().logCustom(
                    new CustomEvent("Sending SMS")
                            .putCustomAttribute("Sms Body",
                                    String.valueOf(textMessage))
                            .putCustomAttribute("To",
                                    String.valueOf(phoneNumber))
                            .putCustomAttribute("Alert Remote Id",
                                    String.valueOf(alertRemoteId))
            );
            App.logException("Sending Message " +
                    "Remote alert id: " + String.valueOf(alertRemoteId) +
                    "To: " + String.valueOf(phoneNumber));

            Alert alert = new Alert();
            alert.setAlertRemoteId(alertRemoteId);
            alert.setBody(textMessage);
            alert.setCreatedAt(new Date().getTime());
            alert.setToAddress(phoneNumber);
            AlertDatabase.getInstance(getApplicationContext()).alertDao().insert(alert);
            sendSMS(textMessage, phoneNumber);
        } catch (Exception exception) {
            Timber.e(exception);
            App.logException(exception);
        }
    }
}