Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMS receive with no notification

I want to receive a sms in my app, but I don't want my Android to show a notification about that event. Algorithm:

  1. Receive sms (it's ok)

  2. If this is a sms with special content-format (for my app) - process it with my app and don't display a notification.

  3. If this is a simple message - I don't want to process it, so a notification must be displayed.

I tried to use an ordered broadcast, but it doesn't help. Somewhere I read that SMS_RECEIVE's broadcast is not ordered, but I saw some apps, which can receive SMS without notify.

Does anyone can help me or show me the right way to solve this problem?

Calling abortBroadcast() in broadcast doesn't help

like image 769
UAS Avatar asked Mar 21 '11 15:03

UAS


3 Answers

set priority in your intent-filter // in manifest

<intent-filter android:priority="100">  /*your receiver get high priority*/

// in broadcast receiver

 if (keyword_match)
      {
        // Stop it being passed to the main Messaging inbox
        abortBroadcast();
      }
like image 159
Pratik Popat Avatar answered Sep 22 '22 05:09

Pratik Popat


This should be possible by registering your app to receive SMS messages and then using abortBroadcast() when you detect your message has arrived. You say abortBroadcast() doesn't work - is the SMS definitely getting handled by your SMS receiver?

For anybody else wanting to do this, read on...

First, declare the SMS receiver in your AndroidManifest.xml and make sure the app has permission to receive SMS messages.

 <receiver android:name="mypackage.SMSReceiver">
   <intent-filter>
     <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
   </intent-filter>
 </receiver>

<uses-permission android:name="android.permission.RECEIVE_SMS" />

Here's some example code to handle the SMS messages:

public class SMSReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    Bundle extras = intent.getExtras();

    Object[] pdus = (Object[])extras.get("pdus");
    for (Object pdu: pdus)
    {
      SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);

      String origin = msg.getOriginatingAddress();
      String body = msg.getMessageBody();

      // Parse the SMS body
      if (isMySpecialSMS)
      {
        // Stop it being passed to the main Messaging inbox
        abortBroadcast();
      }
    }
  }
}
like image 42
Dan J Avatar answered Sep 19 '22 05:09

Dan J


You should not do this. Other apps might want or need to receive the SMS_RECEIVED broadcast. Aborting it will disrupt 3rd party apps from running properly. This is a bad way to program. you should only abort broadcasts that you create, not system broadcasts. I don't know why the Android OS lets you do this.

like image 30
Camille Sévigny Avatar answered Sep 19 '22 05:09

Camille Sévigny