Я добавил OtpBroadcastReceiver и зарегистрировал его, но не могу получить от него сообщения - PullRequest
0 голосов
/ 26 марта 2020

Вот мой фрагмент -

@SuppressLint("HardwareIds")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter = VerifyOtpPresenter(this)
        androidId = Settings.Secure.getString(activity?.contentResolver, Settings.Secure.ANDROID_ID)
        mPresenter.requestOtp(phoneNumber)

        initClickAndTextListeners()
        initOtpCountdownTimer()


    }

    /**
     * Requesting OTP password for our phone number
     */
    override fun requestOtp(phoneNumber: Long) {
        OtpNetworking.requestOtp(phoneNumber, object : OtpNetworking.RequestOtpCallback {
            override fun onSuccess() {
                Toast.makeText(context, getString(R.string.verify_otp_fragment_sms_arrived), Toast.LENGTH_SHORT).show()
                startSmsRetriever()
            }

            override fun onError(reason: String) {
                Toast.makeText(context, reason, Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun startSmsRetriever() {
        val client = SmsRetriever.getClient(context!!)
        val task = client.startSmsRetriever()
        task.addOnSuccessListener {
            Toast.makeText(context, "Successfully added retriever", Toast.LENGTH_SHORT).show()
        }

        task.addOnFailureListener {
            Toast.makeText(context, "Failed to get SMS", Toast.LENGTH_SHORT).show()
        }
    }

Вот мой OtpBroadcastReceiver -

class OtpBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show()
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status?.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val message: String? = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
                }

                CommonStatusCodes.TIMEOUT -> {
                    Toast.makeText(context, "Timeout", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

и мой файл манифеста -


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        <activity android:name=".startup.StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        </activity>

        <receiver android:name=".otp.service.OtpBroadcastReceiver" android:exported="true"
            android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
            </intent-filter>
        </receiver>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

Я могу ' Кажется, я не получаю никакой информации от моего широковещательного приемника, даже если в тост-сообщении смс-ретривера говорится, что успешно добавленный ретривер

Я думаю, что мне не хватает соединения между фрагментом и широковещательным приемником, но я не уверен - У кого-нибудь есть идея, что я скучаю?

...