So, in my app, I respond to incoming sms, and I'd like to be able to respond to SMS sent by an email address, but isEmail() always returns false, therefore getEmailFrom() and getEmailBody() always return null. Here's my code:
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
String tag = "SMS_RECEIVED";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
SmsMessage recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
recMsgString = recMsg.getMessageBody();
fromAddress = recMsg.getOriginatingAddress();
if (recMsg.isEmail()){
fromAddress = recMsg.getEmailFrom();
recMsgString = recMsg.getEmailBody();
}
}
//do some logging
//code to react to the message
}
}
Any help would be greatly appreciated.
Hrm well I have never heard of a string method called isEmail() - maybe I am missing something?
You declare
String recMsgString = "";
and then try to access
recMsgString.isEmail()
Which does not exist. Also I notice you set recMsgString equal to recMsg.getMessageBody()
recMsg.getMessageBody();
I wouldn't think the getMessageBody() function would return an email anyway.
I think what you meant to do was:
SmsMessage recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
...
if (recMsg.isEmail()){
fromAddress = recMsg.getEmailFrom();
recMsgString = recMsg.getEmailBody();
}
I kinda/sorta figured something out, but I'm not sure why it worked...
So, in my app instead of checking isEmail and then using getEmailFrom and getEmailBody, I just got the sender (which is something like 14100000011), and then used recMsgString.contains(myKeyword)
My app then sends a message back to 14100000011 and it delivers to my email.
I'm not really sure why it behaves this way, though. I guess AT&Ts email gateway just delivers back to wherever I sent it from. Didn't know this would work.
On a side note, does anyone know the functioning on any other carrier?
That is, if you send an email from gmail (or another email) to your phone number as such:
AT&T: phonenumber#txt.att.net
T-Mobile: phonenumber#tmomail.net
Verizon: phonenumber#vtext.com
Sprint: phonenumber#messaging.sprintpcs.com
Then reply, do you receive a message back in your email?
If anybody is kind enough to test this, please leave a comment saying who your carrier is and who your email provider is and the results of the test.
Related
I have created an android application that listens for incoming sms. The issue i am encountering is that it also reads previous sms. The goal of the app was to grab sms from a specific originating address and store it in a database.
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i(TAG, "Intent Received: "+intent.getAction());
if (intent.getAction()==SMS_RECEIVED){
Bundle dataBundle = intent.getExtras();
if(dataBundle != null){
//creating PDU protocol Data unit object which is a protocol for transferring message
Object[] mypdu = (Object[])dataBundle.get("pdus");
final SmsMessage[] message = new SmsMessage[mypdu.length];
for(int i =0; i< mypdu.length; i++){
//for build version >= API
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
String format = dataBundle.getString("format");
//From PDU we get all object and smsMessage using following line of code
message[i] = SmsMessage.createFromPdu((byte[])mypdu[i],format);
}else{
message[i] = SmsMessage.createFromPdu((byte[]) mypdu[i]);
}
msg += message[i].getMessageBody().toString().replace("null","");
originatingAddress = message[i].getOriginatingAddress();
}
msg = msg.replace("null","");
if(originatingAddress.trim().equals("MPESA")) {
Toast.makeText(context.getApplicationContext(), "message: " + msg, Toast.LENGTH_SHORT).show();
}
}
}
// throw new UnsupportedOperationException("Not yet implemented");
}
}
Please try with below way
(1)The Simple way you can achieve to store the unique of record on every SMS is timestamp is only unique in this case whenever you get SMS store timestamp of every Sms as a Unique or primary key as you want in database, like system.currentTimeMillisecond to get time of current SMS and store as LONG type Column in your database,
(2) you can also check with unique time on every SMS get but it is complex to check with every existing records
Hope this process will help in your way with prevent of duplicate record store in database
I want to open incoming sms from notification directly to my application's intent , Please help I tried following code to read sms from inbox
public class SmsReceiver extends BroadcastReceiver
{
public static String BODY = "Test";
public static String ADDRESS = "5556";
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
ADDRESS=msgs[i].getOriginatingAddress();
BODY=str;
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
When a new SMS is received by your device, the android creates a intent and broadcasts the SMS_RECEIVED_ACTION. What you are going to do is create an Intent Filter for the activity of your application that handles the SMS that the phone receives.
When the phone gets a new SMS message and the user clicks on the notification, she is presented with a dialog of apps that can handle this action*****. Your app should be there, and when she decides that she wants your app to handle the incoming message then you can use the getMessagesFromIntent method in order to extract the message (you use that method inside your app).
PS: You cannot set your app as the default message app, which you probably need to do. The user has to explicitly choose your app to open the message, and if she wants it to be the default messaging app (by ticking the set as default checkbox).
Note: It looks like you have to create a broadcast receiver that receives the SMS_RECEIVED_ACTION which you can use prior to API 19 as a constant at android.provider.Telephony.SMS_RECEIVED and launch your own notifications in which you direct the user directly to the application of yours. Unfortunately it seems that the default messaging app will still issue notifications.
Credit: #Mike M.
I am trying to build Whatsapp Notification filtering app, where I monitor all notification from Whatsapp and remove messages as per filtering policy.
I can fetch message content using below link code
Extract notification text from parcelable, contentView or contentIntent for first message only
but the problem is I can fetch only first message, if user does not read first message then second message onwards I get only "2 messages from sender" instead of actual message.
NOTE: I am getting
android.text = actual message for first message but its null from second message/notification onwards
android.title = sender
android.summaryText = "n new messages"
any help would be appreciated.
Yes, finally after few hours of googling I design a code which does work for me.
Bundle extras = sbn.getNotification().extras;
CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
JSONArray s = new JSONArray();
for (CharSequence msg : lines) {
msg = removeSpaces(msg);
if (!TextUtils.isEmpty(msg)) {
s.put(msg.toString());
}
}
private static String removeSpaces(#Nullable CharSequence cs) {
if (cs == null)
return null;
String string = cs instanceof String ? (String) cs : cs.toString();
return string.replaceAll("(\\s+$|^\\s+)", "").replaceAll("\n+", "\n");
}
here JSONArray s contains all messages that I want
I search so much for sms filter for hangout enabled android phones where I only found that this is not possible.
But there is one app as sms filter by Tsvetan Nachev which blocks sms from hangout and link for this app is : https://play.google.com/store/apps/details?id=com.nachev.apps.smsfilter
I tested on 4.1.2
So anybody know how does that happening in that app?
here is my code
if (intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0)
{
return;
}
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
if(message.equals("something"))
{
abortBroadcast();
}
}
Thanks in advance.
You May look at this-Intercept Incoming SMS Message and Modify it What you need to do is check when message is came from hangout application it it contains in address or subject that is diff from other message format.For message abort may look the abortBroadcast(); method inside broadcast message receiver in android that receives intent for incoming message.Hope this Helps you.
So im making an app that uses sms to send data between two devices and am trying to get one to automaticly respond to the other. But the string getMessageBody() is returning isnt setting of the if statment to send the automatic message even tho the log output "(test)" seems to match the condition. I have added extra text to either side of the string in the log message to check for whitespace. Thanks in advance.
The code sending the message to be received.
SendSMS(inputNum.getText().toString(),"test");
The receiver code
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if(bundle != null)
{
Object[]pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i = 0;i < msgs.length;i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
originNumber = msgs[i].getOriginatingAddress();
messages = msgs[i].getMessageBody();
Log.e("Received Text", messages);
}
if(messages == "test")
SendSMS(originNumber, "auto send");
else
Log.e("else Text", "("+messages+")");
}
if(messages == "test")
String comparison in java need the equals method. With the == you will compare the address of message with the address of "test". Since those are differente String object the result will be false. Change it in:
if(messages.equals("test"))