Search code examples
androidbroadcastreceiversmsmanager

Broadcast receiver not working properly, onReceive() doesn't get invoked. How should I fix it?


My onReceive() doesn't get invoked at all even when application receives sms message. It doesn't get invoked when the message arrives. Shouldn't it be working in background and only gets invoked when the SMS arrives? It works fine on a seperate project but not when I'm integraing to my own app.

My Code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;


public class SmsBroadcastReciever extends BroadcastReceiver {
    private static final String LOG_TAG = "SMSBroadRec";
    public static final String SMS_BUNDLE = "pdus";
    String SenderNo  = "+92----------";
    String SenderNo2 = "+92----------";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d(LOG_TAG, "In onReceive()");
        Bundle bundle = intent.getExtras();
        String smsBody;
        String address;

        try {

            if ( !bundle.isEmpty() ){
                Log.d(LOG_TAG, "Sms received");
                String verificationCode = null;
                Object[] sms = (Object[]) bundle.get(SMS_BUNDLE);
                for (Object sm : sms) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sm);
                    smsBody = smsMessage.getMessageBody();
                    address = smsMessage.getOriginatingAddress();
                    Log.d(LOG_TAG, address);
                    if (SenderNo.equals(address) || SenderNo2.equals(address)) {
                        verificationCode = getVerificationCode(smsBody);
                        Log.e(LOG_TAG, "OTP received: " + verificationCode);
                        break;
                    } else {
                        Log.d(LOG_TAG, "wrong sender");
                        break;
                    }
                }
                SharedPreferences prefs = context.getSharedPreferences(AppConfig.APP_SCRATCH, Context.MODE_PRIVATE);
                SharedPreferences.Editor ed = prefs.edit();
                ed.putString(AppConfig.APP_SCRATCH, verificationCode);
                if (ed.commit()) {
                    Log.d(LOG_TAG, "commit succesful");                          //added to the shared preferences
                } else {
                    Log.d(LOG_TAG, "commit unsuccessful");
                }

            } else {
                Log.d(LOG_TAG, "Intent must be empty!");
            }

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    private String getVerificationCode(String message) {
        String code = null;
        int index = message.indexOf(AppConfig.OTP_DELIMITER);

        if (index != -1) {
            int start = index + 2;
            int length = 8;
            code = message.substring(start, start + length);
            return code;
        }
        return code;
    }
}

This is the part of the manifest file and i have added permissions too which are READ_SMS, RECEIVE_SMS, WRITE_SMS.

manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ubroadkast.nayatel"
    android:versionCode="4"
    android:versionName="1.3">

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.telephony"
        android:required="true" />

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <uses-permission
        android:name="android.permission.INTERNET"
        android:required="true" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:required="true" />
    <uses-permission
        android:name="android.permission.RECORD_AUDIO"
        android:required="true" />
    <uses-permission
        android:name="android.permission.CAMERA"
        android:required="true" />
    <uses-permission
        android:name="android.permission.READ_PHONE_STATE"
        android:required="true" />
    <uses-permission
        android:name="android.permission.ACCESS_NETWORK_STATE"
        android:required="true" />
    <uses-permission
        android:name="android.permission.SEND_SMS"
        android:required="true" />
    <uses-permission
        android:name="android.permission.ACCESS_WIFI_STATE"
        android:required="true" />
    <uses-permission
        android:name="android.permission.CHANGE_WIFI_STATE"
        android:required="true" />
    <uses-permission
        android:name="android.permission.READ_CONTACTS"
        android:required="true" />
    <uses-permission
        android:name="android.permission.WRITE_CONTACTS"
        android:required="true" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:required="true" />
    <uses-permission android:name="android.permission.WRITE_SMS"
        android:required="true"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"
        android:required="true"/>
    <uses-permission android:name="android.permission.READ_SMS"
        android:required="true"/>



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Ubroadkast">
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="com.facebook.app.FacebookContentProvider912845522101212"
            android:exported="true" />

        <activity
            android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Home"
            android:label="@string/app_name"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Settings"
            android:label="Settings"
            android:screenOrientation="portrait" />
        <activity
            android:name=".UserStatus"
            android:label="@string/title_activity_user_status" />
        <activity
            android:name=".login"
            android:label="@string/app_name"
            android:screenOrientation="portrait" />
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity
            android:name=".facebook"
            android:label="UBroadkast Facebook Sign In"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Terms"
            android:label="Terms And Conditions"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Signup"
            android:label="Sign Up"
            android:screenOrientation="portrait" />
        <activity
            android:name=".rating"
            android:label="Ubroadkast Feedback"
            android:screenOrientation="portrait" />
        <activity
            android:name=".change_password"
            android:label="Reset Password" />
        <activity android:name=".HomeScreen" />
        <activity android:name=".Videoview" />
        <activity android:name=".URLExtractor" />

    <receiver android:name=".SmsBroadcastReciever" android:enabled="true" android:exported="false">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECIEVED"/>
            </intent-filter>

        </receiver>

    </application>

</manifest>

Solution

  • change following line in manifest from

    <action android:name="android.provider.Telephony.SMS_RECIEVED"/>
    

    to

    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    

    There's a spelling mistake of word "received".