Skip to content

Instantly share code, notes, and snippets.

@hgale
Created June 22, 2018 00:23
Show Gist options
  • Save hgale/07bb90db23cb87f2c5dcc9a3f0bf5ddd to your computer and use it in GitHub Desktop.
Save hgale/07bb90db23cb87f2c5dcc9a3f0bf5ddd to your computer and use it in GitHub Desktop.
package org.celo.miner;
import org.celo.miner.SMSLog;
import org.celo.miner.SMSAck;
import org.celo.miner.SMSResponse;
import org.celo.miner.SMSMinerServiceManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Calendar;
import java.util.Date;
import android.os.Parcelable;
import android.app.PendingIntent;
import android.app.Activity;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.Intent;
import android.util.Log;
import android.telephony.SmsManager;
import android.support.v4.content.LocalBroadcastManager;
import java.io.Writer;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Call;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class PushNotificationToSMSService extends FirebaseMessagingService {
public static final String EVENT_NAME_NOTIFY_SMS_LOG = "smsLogNotification";
public static final String EXTRA_SMS_LOGS = "smsLogs";
public static final String EVENT_SMS_SENT = "smsSent";
public static final String SMS_SENT_INTENT = "SMS_SENT";
public static final String USER_PREF_STORE = "User";
private static final String TAG = "PushNotificationToSMSService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "New remote message received");
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message has data payload necessary for sending SMS
if (remoteMessage.getData() == null) {
Log.d(TAG, "No data payload");
return;
}
try {
Map<String, String> data = remoteMessage.getData();
if (data.get("messageId") == null) {
Log.d(TAG, "messageID missing from push notification payload");
return;
}
String messageId = data.get("messageId");
Log.d(TAG, "Processing messageID: " + messageId);
processPushNotificationMessageId(messageId);
}
catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
String s = writer.toString();
Log.d(TAG, "Stacktrace data " + s);
}
}
private void processPushNotificationMessageId(final String messageId) {
if (messageId == null) {
return;
}
Log.d(TAG, "Attempt to fetch message from mining pool using " + messageId);
SMSMinerServiceManager.get().getSms(messageId, new Callback<SMSResponse>() {
@Override
public void onResponse(Call<SMSResponse> call,
Response<SMSResponse> response) {
Log.d(TAG, "Response received from mining pool for " + messageId);
if (response.isSuccessful() && response.body() != null) {
SMSResponse message = response.body();
// If message has not already been sent or if message has not timed out send sms
if (!message.sent && !message.timedOut && message.phoneNumber != null && message.message != null) {
sms(message.phoneNumber, message.message, messageId);
} else {
Log.d(TAG, "Problem occured with one of message " + messageId + " properties.");
}
} else {
Log.d(TAG, "Problem occured with response for message " + messageId);
}
}
@Override
public void onFailure(Call<SMSResponse> call, Throwable t) {
Log.e(TAG, "Failed to fetch message" + messageId +" from mining pool error: " + t.getMessage());
return;
}
});
}
private void sms(String phoneNumberString, String message, final String messageId) {
if (phoneNumberString == null || message == null || messageId == null) {
return;
}
Log.d(TAG, "Send sms to " + phoneNumberString + " containing " + message + " pulled from id: " + messageId);
try {
// This is used to check if SMS actually got sent or failed.
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SMS_SENT_INTENT), 0);
registerReceiver(
new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0,Intent arg1)
{
Log.d(TAG,"SMS_SENT_INTENT onReceive hit with code " + getResultCode());
switch(getResultCode())
{
case Activity.RESULT_OK:
// Ack the message sent to the mining pool
Log.d(TAG,"Message sent, acking with mining pool.");
ackMessageId(messageId);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.d(TAG,"Problem occured sending messages.");
//nackMessageId(messageId);
break;
}
}
}, new IntentFilter(SMS_SENT_INTENT));
// Send SMS
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumberString,null,message,sentPI,null);
Intent smsLogIntent = new Intent(EVENT_NAME_NOTIFY_SMS_LOG);
String pattern = "EEE, d MMM yyyy HH:mm aaa";
DateFormat df = new SimpleDateFormat(pattern);
Date today = Calendar.getInstance().getTime();
// Log SMS to preferences
String sendDate = df.format(today);
SMSLog log = new SMSLog(true, sendDate, phoneNumberString, messageId);
saveSMSLogToSharedPreferences(log);
List<SMSLog> smsLogs = getSMSLogsFromSharedPreferences(getApplicationContext());
String numberOfSMS = Integer.toString(smsLogs.size());
smsLogIntent.putParcelableArrayListExtra(EXTRA_SMS_LOGS, (ArrayList<? extends Parcelable>) smsLogs);
// Broadcast array of all smsLogs from service to module so it can emit them to react native app
LocalBroadcastManager.getInstance(getApplicationContext())
.sendBroadcast(smsLogIntent);
}
catch (Exception e) {
e.printStackTrace();
// Inform mining pool
nackMessageId(messageId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment