Created
October 23, 2017 17:03
-
-
Save amadeu01/cd8a90f9827342df58f88a30d0905749 to your computer and use it in GitHub Desktop.
Service to send SMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment