I have facebook integration done for my android app, now I have the FB login button also user can register for our application and login using his email id.
so I have 2 different ways to login.
Problem is that I m unable to get the values in the EditText field provided by user to login.
I m able to get the value in registeration page but not in the login page.
But if I remove all the codes used for FB integration it works fine..
I have a fragment activity.
Please help and let me know if any info needed..
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
try {
Log.i("TAG", "Inside Hash Key");
PackageInfo info = getPackageManager().getPackageInfo("com.package", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash :", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
}
try {
Log.i("TAG", "Inside Hash Key");
PackageInfo info = getPackageManager().getPackageInfo("com.package", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash :", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
This is what I have in my oncreate.
Related
maybe my code will mess here beacuse I'm new into firebase stuff and there is lack doc for newbie like me, so I want to make save and load data from firebase, there is 2 scene the first for main menu and second one the main game,so when firstime login or auto login, user will load current user data from realtime database (I'm using firebase) also applies to user when back to main menu.
in the second scene itself, user load current user highscore data at first start then when game over if the score > lasthighscore that will save/update latest highscore to realtime database.
here the preview this is when I auto-login then playgame until game over:
it's supposed to be updated but not when back to main menu, I already try with player prefs since i erase all data when sign out so the highscore always be 0 and if login with other account that highscore is not belong to other account.
here my firebase script on main menu:
private void Start()
{
Time.timeScale = 1;
mainPanel.SetActive(true);
InitializeFirebase();
}
void InitializeFirebase()
{
auth = FirebaseAuth.DefaultInstance;
DBreference = FirebaseDatabase.DefaultInstance.RootReference;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}
void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
//This checks if the user (your local user) is the same as the one from the auth
if (auth.CurrentUser != User)
{
//this seems the same, but user could have been null before
bool signedIn = User != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && User != null)
{
Debug.Log("Signed out " + User.UserId);
loginPanel.SetActive(true);
}
//this is important step, this user is the one you should be working with
User = auth.CurrentUser;
if (signedIn)
{
Debug.Log("Signed in " + User.UserId);
userNameShowText.text = User.DisplayName;
StartCoroutine(LoadUserData());
loginPanel.SetActive(false);
// //highScoreMainMenu.text = PlayerPrefs.GetInt("highscore").ToString("0000000");
}
else
{
loginPanel.SetActive(true);
}
}
}
public IEnumerator Login(string _email, string _password)
{
//Call the Firebase auth signin function passing the email and password
var LoginTask = auth.SignInWithEmailAndPasswordAsync(_email, _password);
//Wait until the task completes
yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);
if (LoginTask.Exception != null)
{
//If there are errors handle them
Debug.LogWarning(message: $"Failed to register task with {LoginTask.Exception}");
FirebaseException firebaseEx = LoginTask.Exception.GetBaseException() as FirebaseException;
AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
string message = "Login Failed!";
switch (errorCode)
{
case AuthError.MissingEmail:
message = "Missing Email";
break;
case AuthError.MissingPassword:
message = "Missing Password";
break;
case AuthError.WrongPassword:
message = "Wrong Password";
break;
case AuthError.InvalidEmail:
message = "Invalid Email";
break;
case AuthError.UserNotFound:
message = "Account does not exist";
break;
}
warningLoginText.text = message;
}
else
{
//User is now logged in
//Now get the result
User = LoginTask.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})", User.DisplayName, User.Email);
warningLoginText.text = "";
confirmLoginText.text = "Logged In";
StartCoroutine(LoadUserData());
yield return new WaitForSeconds(2);
userNameShowText.text = User.DisplayName;
UserDataScreen(); // Change to user data UI
confirmLoginText.text = "";
ClearLoginFeilds();
ClearRegisterFeilds();
}
}
public IEnumerator UpdateHighScore(int _highScore)
{
var DBTask = DBreference.Child("users").Child(auth.CurrentUser.UserId).Child("highscore").SetValueAsync(_highScore);
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else
{
//giscrore now updated
}
}
and this is for the game scene also same function for update higscore like on main menu.
void Start()
{
InitializeFirebase();
YetGameOver();
//GetScore
score = 0;
scoreText.text = score.ToString("00000");
//highScoreText.text = "HI :" + PlayerPrefs.GetInt("highscore", 0).ToString("00000");
maxTime = .1f;
}
void InitializeFirebase()
{
auth = FirebaseAuth.DefaultInstance;
DBreference = FirebaseDatabase.DefaultInstance.RootReference;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}
void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
//This checks if the user (your local user) is the same as the one from the auth
if (auth.CurrentUser != User)
{
//this seems the same, but user could have been null before
bool signedIn = User != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && User != null)
{
Debug.Log("Signed out " + User.UserId);
}
//this is important step, this user is the one you should be working with
User = auth.CurrentUser;
if (signedIn)
{
Debug.Log("Signed in " + User.UserId);
StartCoroutine(LoadUserData());
//highScoreMainMenu.text = PlayerPrefs.GetInt("highscore").ToString("0000000");
}
}
}
public void GameOver()
{
SaveData();
StartCoroutine(WaitToDeath());
}
public void SaveData()
{
Debug.Log("Saved");
StartCoroutine(UpdateHighScore(PlayerPrefs.GetInt("highscore", 0)));
}
public IEnumerator WaitToDeath()
{
_CamShake.instance.shouldShake = true;
DeathSound();
gameOverPanel.SetActive(true);
endHighScoreText.text = "HI :" + PlayerPrefs.GetInt("highscore").ToString("0000000");
scoreText.gameObject.SetActive(false);
highScoreText.gameObject.SetActive(false);
yield return new WaitForSeconds(.1f);
//_AdmobAds.instance.ShowInterstitialAd();
isStarted = false;
isGameOver = true;
Time.timeScale = 0;
}
//same function like main menu
public IEnumerator UpdateHighScore(int _highScore)
{
var DBTask = DBreference.Child("users").Child(auth.CurrentUser.UserId).Child("highscore").SetValueAsync(_highScore);
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else
{
//highscore are now updated
}
}
public IEnumerator LoadUserData()
{
//Get the currently logged in user data
var DBTask = DBreference.Child("users").Child(User.UserId).GetValueAsync();
yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
if (DBTask.Exception != null)
{
Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else if (DBTask.Result.Value == null)
{
//No data exists yet
highScoreText.text = "0";
endHighScoreText.text = "0";
}
else
{
//Data has been retrieved
DataSnapshot snapshot = DBTask.Result;
highScoreText.text = endHighScoreText.text = snapshot.Child("highscore").Value.ToString();
}
}
You don't capture child in this method. You can only capture all the data from the root. So, simply remove the child. It will work.
my solution to firebase realtime database not working was that my google-services.json wasnt updated after adding the google-services.json from firebase auth. firebase, project overview cogwheel, project settings, json on that page somewhere.
and my problem with firebase auth before that was that i didnt set the Client ID in unity, window, google play games, setup, android setup.. get the id from google play console, play games services, setup & management, configuration, and figure out how to add game server credentials
Whenever I am opening my app I need to check the location is on or off. If the location is off, I need to show an alert to the user to enable location share like the below screenshot:
I try this using the dependency service from this thread:
The interface on shared Project:
public interface ILocSettings
{
void OpenSettings();
}
Android implementation
[assembly: Dependency(typeof(LocationShare))]
namespace ProjectName.Droid
{
public class LocationShare : ILocSettings
{
public void OpenSettings()
{
//code1
Android.App.Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));
//code2
//LocationManager LM = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
//if (LM.IsProviderEnabled(LocationManager.GpsProvider) == false)
//{
// Context ctx = Android.App.Application.Context;
// ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));
//}
}
}
}
Finally from the shared project called like below:
var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK", "CANCEL");
if (myAction)
{
if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
{
DependencyService.Get<ILocSettings>().OpenSettings();
}
}
else
{
await DisplayAlert("Alert", "User Denied Permission", "OK");
}
I am getting below exception when running this. (Getting the same exception for code1 and code2)
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I need to show the alert only if the location is off. If the location is on, no need to do these things. How I can check the location is on or off?
Also, I need to implement the same feature for ios and windows platforms.
Update 1
Hi #Lucas Zhang - MSFT
I have tried your solution and got an alert like this. But after giving the location access, still the device's location is off. I need to on the device's location like this when the user taps the OK option in the alert (question screenshot). Either on the location directly or redirect to the location settings page.
Update 2
Tried GeolocatorPlugin and used the below code for checking the GPS is off or on. Always getting False value even if the GPS is on.
public bool IsLocationAvailable()
{
if (!CrossGeolocator.IsSupported)
return false;
return CrossGeolocator.Current.IsGeolocationAvailable;
}
Made below modification on the android service and now I am able to open the location settings.
public class LocationShare : ILocSettings
{
public void OpenSettings()
{
Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
Before opening the location settings page, I need to verify the GPS is on or off (not the location permission).
Also I didn't understand the ios answer by Jack. So can you show me the ios dependency service like I did for android for opening ios location settings page?
In your case you could use the plugin PermissionsPlugin from Nuget.
Usage
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
}
if (status == PermissionStatus.Granted)
{
//Query permission
}
else if (status != PermissionStatus.Unknown)
{
//location denied
}
}
catch (Exception ex)
{
//Something went wrong
}
Update
It seems that you want to check if system location is open or not , right ? If so , you could try to achieve GPS info after you get the location permission . If the GPS info is still unavailable , that means the system setting is OFF .And you can invoke dependency service to open platform setting page.
public async void ShareLocation()
{
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (status == PermissionStatus.Granted)
{
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
if (!gpsStatus)
{
var myAction = await DisplayAlert("Location", "Please turn on GPS for the proper working of the application.", "TURN ON", "CANCEL");
if (myAction)
{
DependencyService.Get<ILocSettings>().OpenSettings();
}
}
}
}
//ILocSettings
public interface ILocSettings
{
void OpenSettings();
bool isGpsAvailable();
}
//Android Dependency Service
[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.Droid.Services
{
public class LocationShare : ILocSettings
{
public bool isGpsAvailable()
{
bool value = false;
Android.Locations.LocationManager manager = (Android.Locations.LocationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
if (!manager.IsProviderEnabled(Android.Locations.LocationManager.GpsProvider))
{
//gps disable
value = false;
}
else
{
//Gps enable
value = true;
}
return value;
}
public void OpenSettings()
{
Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
}
For iOS
public void CheckAuthorization(CLLocationManager manager, CLAuthorizationStatus status)
{
switch (status)
{
case CLAuthorizationStatus.Authorized | CLAuthorizationStatus.AuthorizedAlways | CLAuthorizationStatus.AuthorizedWhenInUse:
Console.WriteLine("Access");
break;
case CLAuthorizationStatus.Denied | CLAuthorizationStatus.NotDetermined | CLAuthorizationStatus.Restricted:
Console.WriteLine("No Access");
break;
default:
Console.WriteLine("No Access");
break;
}
}
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
The following 3 functions are used to set up email and other auth checks (called in order)
private void buildSignInIntentBuilder() {
ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()
.setAndroidPackageName(getString(R.string.packageName), true, null)
.setHandleCodeInApp(true)
.setUrl(getString(R.string.dynamic_link_url))
.build();
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder()
.enableEmailLinkSignIn()
.setActionCodeSettings(actionCodeSettings)
.build(),
// new AuthUI.IdpConfig.EmailBuilder().setRequireName(false).build(),
new AuthUI.IdpConfig.PhoneBuilder()
.build(),
new AuthUI.IdpConfig.GoogleBuilder()
.build(),
new AuthUI.IdpConfig.FacebookBuilder()
.build());
signInIntentBuilder = AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(providers)
.setLogo(R.drawable.icon_forget_me_not_1);
}
private void catchEmailLinkSignIn() {
Log.d(TAG, "Intent: " + getIntent().getExtras());
if (AuthUI.canHandleIntent(getIntent())) {
if (getIntent().getExtras() == null) {
return;
}
String link = getIntent().getExtras().getString(ExtraConstants.EMAIL_LINK_SIGN_IN);
Log.d(TAG, "link: " + link);
if (link != null) {
signInIntentBuilder.setEmailLink(link);
}
}
}
private void createCheckAndSigninListener() {
// set firebase sign in listener
mAuthStateListner = firebaseAuth -> {
// Already logged in
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "user already signed in");
// Check user even if signed in to register him to database (if haven't)
FirebaseAuthHelper.getInstance().checkRegisterUser(user, this, CHECK_USER_DB);
} else {
Log.d(TAG, "user hasn't signed in");
// Signed out or hasn't logged in
startActivityForResult(
signInIntentBuilder
.build(),
RC_SIGN_IN
);
}
};
}
I have set up a dynamic link with firebase hosting. And being able to redirect into the same activity upon clicking the received email link.
However,
String link = getIntent().getExtras().getString(ExtraConstants.EMAIL_LINK_SIGN_IN);
Log.d(TAG, "link: " + link); // --> produces "link: null"
Shows despite successfully getting a intent, there is no EMAIL_LINK_SIGNIN extra in the getExtras(). I spend few hours looking into the source code of FirebaseUi, but I didn't find where the constant EMAIL_LINK_SIGN_IN is used and how is the intent from dynamic link parsed.
Any idea how to fix this problem is appreciated. I had already spent a whole day trying to figure this out.
Instead of using String link = getIntent().getExtras().getString(ExtraConstants.EMAIL_LINK_SIGN_IN);
use getIntent().getData().toString(); instead.
I am working on Push notifications for iPhone. I am sending notification on button click, but the iPhone developer is not receiving the alert message. Can anyone plz help me?
This is my method --
protected void btnPush_Click(object sender, EventArgs e)
{
string devicetoken = "bhsdse78 d52c6a34 273de5f7 27947945 24736e36 33d93a6c 3147a416 434995eb";
try
{
if (devicetoken != "")
{
//lblError.Visible = false;
string p12FileName = "D:/Worksapace/Coupzila/Certificates(3).p12"; // change this to reflect your own certificate
string p12Password = "seas"; // change this
bool sandBox = true;
int numConnections = 1; // you can change the number of connections here
var notificationService = new NotificationService(sandBox, p12FileName, p12Password, numConnections);
var deviceToken = devicetoken; // put in your device token here
var notification = new Notification(deviceToken);
notification.Payload.Alert.Body = "Hi this is push notification test message.";
notification.Payload.Sound = "default";
notification.Payload.Badge = 1;
notification.Payload.HideActionButton = true;
if (notificationService.QueueNotification(notification))
{ }
else
{ }
// This ensures any queued notifications get sent befor the connections are closed
notificationService.Close();
notificationService.Dispose();
}
else
{
//lblError.Visible = true;
}
}
catch (Exception ex)
{
}
}
This method is running successfully, but not getting notifications on iPhone, Whtz problem in my code?
Help me out, Thanks in advance
First, Remove the empty characters from your deviceToken.
Second, Put # sign for your fileName;
string p12FileName = #"D:/Worksapace/Coupzila/Certificates(3).p12";
And for the last step implement your catch, like;
catch (Exception ex)
{
MessageBox.Show("There is an error from push notification. Details:\n" + ex);
}
And good with Apple.
i did my coding in console and used a system.console. write line in almost every if/else statement so as to display an error message if wrong values are entered of to say if what goes wrong. how ever am trying to apply the same analogy in web forms such that should there be a user error while running the conditions, an error message may be displayed to the user on the screen.
How do i go about displaying the errors?i know of item validations but cant apply that using if and else. however if i use try catch, i dont know which code displays the error message to user. but i know for sure i cant use SC.writelines.
a sample of my code is below.please advice..
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string strConn;
string userType;
strConn = "Provider=MIcrosoft.Jet.OLEDB.4.0;data Source=" +
Server.MapPath("App_Data/test.mdb");
OleDbConnection mDB = new OleDbConnection(strConn);
mDB.Open();
userClass aUser = new userClass();
if (aUser.verifyUser(mDB, Login1.UserName, Login1.Password))
{
userType = aUser.getUserDesc();
if (userType.ToLower() == "customer")
{
Response.Redirect("StaffMenu.aspx");
}
else if (userType.ToLower() == "front desk")
{
Response.Redirect("StaffMenu.aspx");
}
else if (userType.ToLower() == "technician")
{
Response.Redirect("StaffMenu.aspx");
}
else if (userType.ToLower() == "admin")
{
Response.Redirect("StaffMenu.aspx");
}
}
else
{
e.Authenticated = false;
}
mDB.Close();
when the program moves to the User class to run the verify method, it does so by running the following bock of code..
public bool verifyUser(OleDbConnection mDB, string userIDStr, string userPwrdStr)
{
string sqlQuery;
OleDbCommand cmd;
OleDbDataReader rdr;
//SC.Write("\n*******User Login********\nEnter User ID:");
//userIDStr = userIDInt.ToString();
//SC.Write("\nEnter User Password:");
//userPwrdStr = userPwrdStr;
sqlQuery = "SELECT UserID, UserPassword, UserDescription FROM UserTable WHERE UserID = " +
toSql(userIDStr);
cmd = new OleDbCommand(sqlQuery,mDB);
//Boolean valid = false;
//Boolean HasRows = false;
try
{
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
if (userIDStr == (string)rdr["UserID"])
{
if (userPwrdStr == (string)rdr["UserPassword"])
{
userDescStr = (string)rdr["UserDescription"];
if (userDescStr.ToLower() == "admin")
{
//SC.WriteLine("Welcome to Admin Main Menu");
return true;
}
else if (userDescStr.ToLower() == "front desk")
{
//SC.WriteLine("Welcome to Front Desk Staff Main Menu");
return true;
}
else if (userDescStr.ToLower() == "technician")
{
//SC.WriteLine("Welcome to Technical Staff Main menu");
return true;
}
else if (userDescStr.ToLower() == "customer")
{
//SC.WriteLine("Sorry, Customers are not allowed access to the Administrative page");
return true;
}
}
else
{
//SC.WriteLine("\nInvalid User Password, Please try again");
//verifyUser(mDB);
}
}
else
{
//SC.WriteLine("Invalid User ID, Please try again");
//verifyUser(mDB);
}
//HasRows = true;
}
rdr.Close();
}
catch (Exception ex)
{
SC.WriteLine(ex.Message);
}
return false;
}//=================================end verify User()
how ever if the wrong username or password is entered, how can i display an error message to the user as to which of the controls is receiving the wrong value...
am hoping to use this code to replace my SC.writelines that displays messages to the user....
You would probably build up a list of errors in a string and assign them to a label on the form, or potentially a ul tag.
I'd also recommend not differentiating whether or not a username or the password is invalid. If they don't manage to login, you should just display a 'Could not log in' message. If they get a correct username and an incorrect password, then using your method, I would know that I had guessed a correct username and could hammer away at passwords for that.
In addition, have you looked at the built in ASP.net login controls and the membership providers? They take care of a lot of this for you, or you can roll your own that integrates with the supplied controls.