This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.google.android.gms:play-services-auth:17.0.0' | |
implementation 'com.google.android.gms:play-services-auth-api-phone:17.1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.google.android.gms:play-services-auth:17.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface SMSListener { | |
void onSuccess(String message); | |
void onError(String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// This block for request hint --> getting user phone number. | |
if (requestCode == RESOLVE_HINT) { | |
if (resultCode == RESULT_OK) { | |
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY); | |
if (credential != null) { | |
// credential.getId(); <-- will need to process phone number string | |
// sendPhoneNumberToServer(credential.getId()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start listening for SMS User Consent broadcasts from senderPhoneNumber | |
// The Task<Void> will be successful if SmsRetriever was able to start | |
// SMS User Consent, and will error if there was an error starting. | |
SmsRetriever.getClient(context).startSmsUserConsent(senderPhoneNumber /* or null */); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver android:name=".MySMSBroadcastReceiver" android:exported="true"> | |
<intent-filter> | |
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/> | |
</intent-filter> | |
</receiver> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SmsRetrieverClient client = SmsRetriever.getClient(this); // starting the SmsRetriever API | |
Task<Void> task = client.startSmsRetriever(); | |
task.addOnSuccessListener(aVoid -> { | |
MySMSBroadcastReceiver.initSMSListener(new SMSListener() { | |
@Override | |
public void onSuccess(String message) { | |
if (message != null) { | |
// get the message here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* BroadcastReceiver to wait for SMS messages. This can be registered either | |
* in the AndroidManifest or at runtime. Should filter Intents on | |
* SmsRetriever.SMS_RETRIEVED_ACTION. | |
*/ | |
public class MySMSBroadcastReceiver extends BroadcastReceiver { | |
private static SMSListener smsListener; | |
@Override | |
public void onReceive(Context context, Intent intent) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Obtain the phone number from the result | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == RESOLVE_HINT) { | |
if (resultCode == RESULT_OK) { | |
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY); | |
// credential.getId(); <-- will need to process phone number string | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void requestHint() { | |
HintRequest hintRequest = new HintRequest.Builder() | |
.setPhoneNumberIdentifierSupported(true) | |
.build(); | |
PendingIntent intent = Credentials.getClient(this).getHintPickerIntent(hintRequest); | |
try { | |
startIntentSenderForResult(intent.getIntentSender(), |