How to send otp code sms in firebase with java android? - java

I have a Android Studio project with firebase, I already implement firebase correctly.
im trying to make a sign in with sms authentication, everything works but no sms arrives
I already configure firebase authentication
here's some code
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Toast.makeText(MainActivity.this, "Verification Complete", Toast.LENGTH_SHORT).show();
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(MainActivity.this, "Verification Failed", Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
Toast.makeText(MainActivity.this, "Code Sent", Toast.LENGTH_SHORT).show();
}
};
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_send_otp:
PhoneAuthProvider.getInstance().verifyPhoneNumber(
etPhone.getText().toString(), // Phone number to verify
1, // Timeout duration
TimeUnit.MINUTES, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
break;
case R.id.bt_resend_otp:
break;
case R.id.bt_verify_otp:
break;
}
}
when I press the send otp button the toast "Code Sent" appears
Thanks for the help :)

Since it is a test number, OTP will not arrive. Though for testing you can use the verification code you added with the test phone number on the console.
Firebase Console Dashboard -> Authentication -> SignIn Method -> Phone -> Phone numbers for testing.
Check out the docs for Firebase Phone Verification for Android : https://firebase.google.com/docs/auth/android/phone-auth?authuser=1#test-with-whitelisted-phone-numbers

Related

Amazon IAP not launching purchase dialog

I'm migrating an app from the play store to the Amazon App store, considering that in this way it will be avilable for Windows 11 too.
For this to make something very fast and easy i made an activity called PurchaseActivity which contains the codes brought by the amazon IAP guide PDF.
The activity is called from a "buy now" button of a dialog window with the following code:
public class PurchaseActivity extends Activity {
String parentSKU = "com.amazon.sample.iap.subscription.mymagazine";
//Define UserId and MarketPlace
private String currentUserId;
private String currentMarketplace;
private ProgressDialog progress;
#Override
protected void onStart(){
super.onStart();
progress = new ProgressDialog(this);
progress.setTitle("Purchasing");
progress.setMessage("Wait while making the purchase...");
progress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
progress.dismiss();//dismiss dialog
finish();
}
});
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
PurchasingService.registerListener(this, purchasingListener);
PurchasingService.purchase(parentSKU);
}
#Override
protected void onResume() {
super.onResume();
//getUserData() will query the Appstore for the Users information
PurchasingService.getUserData();
//getPurchaseUpdates() will query the Appstore for any previous purchase
PurchasingService.getPurchaseUpdates(true);
//getProductData will validate the SKUs with Amazon Appstore
final Set<String> productSkus = new HashSet<String>();
productSkus.add(parentSKU);
PurchasingService.getProductData(productSkus);
Log.v("Validating SKUs", "Validating SKUs with Amazon");
}
PurchasingListener purchasingListener = new PurchasingListener() {
#Override
public void onUserDataResponse(UserDataResponse response) {
final UserDataResponse.RequestStatus status = response.getRequestStatus();
switch (status) {
case SUCCESSFUL:
currentUserId = response.getUserData().getUserId();
currentMarketplace = response.getUserData().getMarketplace();
Log.v("IAP SDK", "loaded userdataResponse");
break;
case FAILED:
case NOT_SUPPORTED:
// Fail gracefully.
Log.v("IAP SDK", "loading failed");
break;
}
}
#Override
public void onProductDataResponse(ProductDataResponse productDataResponse) {
switch (productDataResponse.getRequestStatus()) {
case SUCCESSFUL:
//get informations for all IAP Items (parent SKUs)
final Map<String, Product> products = productDataResponse.getProductData();
for (String key : products.keySet()) {
Product product = products.get(key);
Log.v("Product:", String.format("Product: %s\n Type: %s\n SKU: %s\n Price: %s\n Description: %s\n", product.getTitle(), product.getProductType(),
product.getSku(), product.getPrice(), product.getDescription()));
}
//get all unavailable SKUs
for (String s : productDataResponse.getUnavailableSkus()) {
Log.v("Unavailable SKU:" + s, "Unavailable SKU:" + s);
}
break;
case FAILED:
Log.v("FAILED", "FAILED");
progress.dismiss();
finish();
break;
}
}
#Override
public void onPurchaseResponse(PurchaseResponse purchaseResponse) {
switch (purchaseResponse.getRequestStatus()) {
case SUCCESSFUL:
PurchasingService.notifyFulfillment(purchaseResponse.getReceipt().getReceiptId(),
FulfillmentResult.FULFILLED);
break;
case FAILED:
progress.dismiss();
finish();
break;
}
}
#Override
public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse response) {
// Process receipts
switch (response.getRequestStatus()) {
case SUCCESSFUL:
for (final Receipt receipt : response.getReceipts()) {
// Process receipts
if (!receipt.isCanceled()) {
// sharedprefs
SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor sharedPrefEditor = sharedPreference.edit();
sharedPrefEditor.putBoolean("isPro",true);
sharedPrefEditor.apply();
progress.dismiss();
finish();
}
}
if (response.hasMore()) {
PurchasingService.getPurchaseUpdates(true);
}
break;
case FAILED:
Log.d("FAILED", "FAILED");
progress.dismiss();
finish();
break;
}
}
};
}
Yeah i know i should not call all that stuff in the onStart() method but i'll make an onCreate() with a UI later.
As you can see from this code, i'm testing in sandbox mode.
THE PROBLEM: Actually when the activity starts, i see progressDialog, and i read in the debug logs that "V/Validating SKUs: Validating SKUs with Amazon" but i don't see then the amazon buy window. It also seems like the listener code is never called, even if i put some breakpoints in there, they're never reached which is very weird considering that apprently it gets initialized and called successfully by the method "PurchasingService.registerListener(this, purchasingListener)"
Any help would be very apreciated!
Thanks and have a good evening
You need to call registerListener in your Application's onCreate, otherwise Amazon fails to detect there's an Activity being displayed and it will not show the purchase dialog.

Firestore Database Registration Failed Permission Denied But Actually Successfully Registers

So I am having a rather ambiguous issue with my database. Everything was working fine and anytime I would register or somebody would register on my app, it would make the messasge "Registration Successful!" pop up.
I have been updating my code daily and working on changing things in the app and around the database. Today while registering a test user, I got this message -
It claims that registration failed, but the account was successfully registered. And I have tested this with 3 more accounts. Same message - registration, however, is actually successful and the user can log in with his details.
I have not coded this exception. I have coded exceptions such as password too short, registration failed, no internet, etc, but never have I created a Permission check. I think the Permission Denied comes from the +e.getMessage()? Could I have changed something inside the firestorm database rules?
This is my code:
private void showREGISTERnew() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use email to register.");
LayoutInflater inflater = LayoutInflater.from(this);
View layout_login = inflater.inflate(R.layout.layout_login, null);
final MaterialEditText editEmail = layout_login.findViewById(R.id.editEmail);
final MaterialEditText editPassword = layout_login.findViewById(R.id.editPassword);
dialog.setView(layout_login);
//set btton
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//check validation
if(TextUtils.isEmpty(editEmail.getText().toString())){
Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(editPassword.getText().toString())){
Snackbar.make(rootLayout, "Please enter your password", Snackbar.LENGTH_SHORT)
.show();
return;
}
if(editPassword.getText().toString().length() < 6){
Snackbar.make(rootLayout, "Password too short.", Snackbar.LENGTH_SHORT)
.show();
return;
}
//reg new user
auth.createUserWithEmailAndPassword(editEmail.getText().toString(), editPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
//save user to db
User user = new User();
user.setEmail(editEmail.getText().toString());
//user setphone
//user setname
user.setPassword(editPassword.getText().toString());
//use email to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "You have registered successfully.", Snackbar.LENGTH_SHORT)
.show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout, "Registration failed."+e.getMessage(), Snackbar.LENGTH_SHORT)
.show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout, "Registration failed."+e.getMessage(), Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
I actually implemented two databases. One that will hold the userbase ad one that will hold price and location data.
However, I forgot that after a certain period of time, read and write permissions will be automatically set to false.
When I got the notification weeks ago, I changed back the read and write permissions to true on the main database, but forgot about the second one.
I checked the second one and the permissions were set to false, thus giving me registration failed, permission denied even though I could register.
After changing them back to true, application works flawlessly.

Android: How to login an user with his phone number

I'm creating an app with where I want already signed up users to be redirected to their profile if their phone number is registered in the app, but if he is a new user then he will be redirected to the welcome page.
The problem is, I'm able to get the verification code for the phone number, but it is always redirecting to the welcome page irrespective of whether it is an existing user or a new user.
By far I've implemented Firebase phone authentication, and
private void verifyCode(String code) {
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, code);
signInWithCredentials(phoneAuthCredential);
}
private void signInWithCredentials(PhoneAuthCredential phoneAuthCredential) {
mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
final String num = getIntent().getStringExtra("phoneNumber");
final Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("birthday").equalTo(num);
if (task.isSuccessful()) {
if (num.equals(query.toString())) {
Log.i("Method", "Inside if block");
Log.i("value", num + query.toString());
Intent intent = new Intent(VerifyPhoneActivity.this, WelcomeActivity.class);
startActivity(intent);
} else {
Log.i("Method", "Inside if block2");
Log.i("value", num + query.toString());
Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
} else {
Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
I'm taking only the phone number of the user to log in/sign up, and there's no separate button for login and sign up. It should work in such a way that, once the OTP sent by the Firebase auth has been verified and if the phone number is already present in the database, it should directly fo the user's profile page instead of the welcome screen, however, if the phone number is new it should go to the welcome page instead of the user profile page.
The challenge I'm facing is I'm not able to check if the number entered is already present in the database in the user table(I've created a separate user table to store the details of the user when he signs up for the first time).
from what I understood you're able to register users but you're not able to redirect unregistered users to the login/signup screen, if that's the case then you can try to use
FirebaseAuth.getInstance().currentUser
in the on create view method in your welcome/home page it should be something like this
override fun onCreate(savedInstanceState: Bundle?) {
if (FirebaseAuth.getInstance().currentUser == null) {
startActivity(Intent(applicationContext, RegistrationActivity::class.java))
finish()
return
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// do your other stuff
}
this code is written in kotlin it shouldn't be that different in java. hope it helps.
UPDATE------
you have a few mistakes with your code, try this
private void verifyCode(String code) {
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, code);
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final String num = getIntent().getStringExtra("phoneNumber");
FirebaseDatabase.getInstance().getReference("users").orderByChild("phoneNumber").equalTo(num).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
FirebaseDatabase.getInstance().getReference("users").push().child("phoneNumber").setValue(num).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Intent intent = new Intent(VerifyPhoneActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
} else {
Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
first of all, you have to log in the user with the phone detail then you check if you have any user registered with that phone number if you do then that means that this user has already entered the app but if you didn't have any information of user with that phone number then you create his information and direct him to the welcome screen after creating his information, so if this user comes back again you will check if we have a record of user with that phone number (which we do) so this time he will be directed to the profile screen

How to verify multiple phone numbers using Firebase OTP Verification without signing in?

I am currently making an Android App where I have to verify each and every person's Phone Number using OTP who is entering the premises. The user operating this is already signed in the app and now needs to verify the phone number of each person entering the premises. How do I do this without signing each time I receive OTP?
I have tried using the regular Firebase OTP Verification but that signs my user in. I don't want that to happen.
This is my mCallbacks Function
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Toast.makeText(getApplicationContext(), "Verification Complete", Toast.LENGTH_SHORT).show();
showMessage("Success!!","OTP verified!" + credential);
cred = credential;
//btn_add_guest.setEnabled(true);
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(getApplicationContext(), "Verification Failed", Toast.LENGTH_SHORT).show();
Log.i(TAG,"Error is "+e.getMessage());
}
#Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_SHORT).show();
mVerificationId = verificationId;
mResendToken = token;
Log.i(TAG,"VERFICATION ID IS"+mVerificationId);
Log.i(TAG,"RESEND TOKEN"+mResendToken);
}
};

Toast is not showing- emulated with genymotion

I'm trying to develop an android app with android studio 2.3.3 and genymotion 2.10.0
I'm using toast msg to show the firebase connection result but it's not displayed despite there are no errors.
here is my code
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed !",
Toast.LENGTH_SHORT).show();
setContentView(R.layout.failure);
finish();
}
else{
setContentView(R.layout.success);
Intent settings = new Intent(LoginActivity.this,SettingsActivity.class);
startActivity(settings);
finish();
}
}
});
It doesn't show because you finish the Activity after the toast
you can change your ActivityContext to applicationContext
Toast.makeText(getApplicationContext(), "Authentication failed!" , Toast.LENGTH_SHORT).show();

Categories

Resources