Facebook SDK on Android: WebDialog closing after giving authorization - java

I'm creating an app for Android and I've integrated the Facebook SDK to present the users a Feed Dialog with the following code:
public void showDialog()
{
Bundle parameters = new Bundle();
parameters.putString("description", "description about link");
parameters.putString("link", "http://google.nl");
parameters.putString("name","Name of link");
parameters.putString("caption","describe your caption text");
parameters.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = new WebDialog.FeedDialogBuilder(this, Session.getActiveSession(), parameters).build();
feedDialog.show();
}
I call this method after I've created the session:
public void onClick(View v)
{
Session.StatusCallback callback = new Session.StatusCallback()
{
#Override
public void call(Session session, SessionState state, Exception exception)
{
if(session.isOpened()) showDialog();
}
};
Session.openActiveSession(this, true, callback);
}
The WebDialog comes up and I can login. Then It says that I've already authorized this app but after that, the dialog closes without a warning or something. I don't know why this is the case: all the parameters seems to be ok. Can anyone help me with this and explain why the dialog disappears?

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.

Android app terminates after certain time when minimized

I have this app which parses SMS and then converts them into Audio. My app users usually minimize the app and runs it all the time. But my app is getting terminated after sometime. How can i make sure my app will run till a user "terminates" it. Since the core functionality of the app is to convert SMS to audio, i need it running all the time.How can i do this ?
My current MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txtGateway, txtTime, txtAmount;
Speakerbox speakerbox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textView = findViewById(R.id.txt_message);
speakerbox = new Speakerbox(getApplication());
txtAmount = findViewById(R.id.tv_amount);
txtGateway = findViewById(R.id.tv_gateway);
txtTime = findViewById(R.id.tv_time);
requestSmsPermission();
}
#Override
public void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("otp"));
super.onResume();
}
#Override
public void onPause() {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("otp"));
super.onPause();
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("otp")) {
final String message = intent.getStringExtra("message");
String gateway = intent.getStringExtra("gateway");
String time = intent.getStringExtra("time");
String amount = intent.getStringExtra("amount");
speakerbox.play(message);
txtGateway.setText(gateway);
txtTime.setText(time);
txtAmount.setText(amount);
// message is the fetching OTP
}
}
};
/**
* Requesting multiple permissions (storage and location) at once
* This uses multiple permission model from dexter
* On permanent denial opens settings dialog
*/
private void requestSmsPermission() {
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener() {
#Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
// Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show();
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
// show alert dialog navigating to Settings
showSettingsDialog();
}
}
#Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
#Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
}
})
.onSameThread()
.check();
}
/**
* Showing Alert Dialog with Settings option
* Navigates user to app settings
* NOTE: Keep proper title and message depending on your app
*/
private void showSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Permissions");
builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
openSettings();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
// navigating user to app settings
private void openSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 101);
}
}
Use a foreground service to ensure that your app is not killed by Android. On newer version of Android, app's background process gets killed after sometime. Having a foreground service will ensure that your app stays active. Keep the service on background thread and not on the main thread.
Read more at:
https://developer.android.com/guide/components/services
and
https://androidwave.com/foreground-service-android-example/
Activities are used for user facing parts of your application. Use service instead. Most likely, your application is getting terminated by os due to low memory situation.
Use service for the core functionality if you want to run all the time in background.If user is not using your app actively yet app is taking RAM memory then OS will terminate the app to avoid out of memory.

Facebook SDK Graphrequest returns null

Using Facebook SDK 4.4 in Xamarin.Android project. I have the following method :
protected override void OnCreate(Bundle savedInstanceState)
{
LoginButton button = FindViewById<LoginButton>(Resource.Id.FbLoginBtn);
button.SetReadPermissions(new List<string>{ "public_profile", "email" });
mCallBackManager = CallbackManagerFactory.Create();
button.RegisterCallback(mCallBackManager, this);
button.Click += (o, e)=>
{
GraphRequest request = GraphRequest.NewMeRequest(AccessToken.CurrentAccessToken, this);
Bundle paramenters = new Bundle();
paramenters.PutString("fields", "id,name,email,first_name,last_name");
request.Parameters = paramenters;
request.ExecuteAsync();
};
}
public void OnCompleted(JSONObject #object, GraphResponse response)
{
if (#object != null)
{
userEmail.Text = #object.ToString();
}
}
The problem with the above code is that, in the OnCompleted method, the JSONObject always return null when i log in to my facebook account from the app. But when i click the button again to Log Out, i get the proper json with all fields that i specified.
My question is, why is the GraphRequest returning null upon log-in but successfully returns the JSON upon log out?

Create Session with Android Java Firebase

I am using email and password. I have create user and authenticate user but I don't know how to add a session for this user.
For example, if the user logs into his/her account. He/she is able to delete the application's process on their phone when they do not want to use the app, then get rid of the app's process and the session is should still be ongoing, therefore when they go back to their application they should still be logged in until he/she logs out (unauth).
I am having trouble making a session for a logged in user. I believe a token must be used but I have no idea how I should use it.
Login Activity:
Firebase user_data = new Firebase("https://myapp.firebaseio.com");
user_data.authWithPassword(login_email.getText().toString(), login_pwd.getText().toString(), new Firebase.AuthResultHandler() {
#Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
Toast.makeText(getBaseContext(), "Login success!", Toast.LENGTH_LONG).show();
Intent toMainActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(toMainActivity);
}
#Override
public void onAuthenticationError(FirebaseError firebaseError) {
// there was an error
System.out.println("ERROR.........................................");
}
});
Heres a simple scenerio:
User logs in. (From Login class is being Intent to Main activity class)
User does not log out but delete app's process.
Later User decides to use the app.
My problem: When click on app, it brings the user back to the Login page whereas it should brought the user to the Main Activity page.
Updated - Initialization
I have initialize it's still not saving the logged in state.
My problem: When click on app, it brings the user back to the Login page whereas it should brought the user to the Main Activity page.
Here's the Main Activity page:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_main);
Firebase user_data = new Firebase("https://myapp.firebaseio.com");
user_data.addAuthStateListener(new Firebase.AuthStateListener() {
#Override
public void onAuthStateChanged(AuthData authData) {
if (authData != null) {
System.out.println("Authentication is currently working"); //this did print
} else {
System.out.println("Failed authentication");
}
}
});
AuthData authData = user_data.getAuth();
if (authData != null) {
System.out.println("The state is: " + authData); //this did print
} else {
System.out.println("Failed");
}
I check the authentication and they seem to be fine but when I delete the process after logging in at the Main Activity it jumps back to the Login page when I reload the app.
The results for monitoring the auth data above:
working auth
Authentication is currently working
state
The state is: AuthData{uid='simplelogin:3', provider='password', token='***', expires='1426758087', auth='{provider=password, uid=simplelogin:3}', providerData='{email=du16493#gmail.com, isTemporaryPassword=false}'}
Authentication is currently working
SOLVED
Just add the intent if authentication is currently running and it should straight back into the Main activity when the app first loads up on your phone at your first activity you called.
Here's the Login Activity page:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_main);
Firebase user_data = new Firebase("https://myapp.firebaseio.com");
user_data.addAuthStateListener(new Firebase.AuthStateListener() {
#Override
public void onAuthStateChanged(AuthData authData) {
if (authData != null) {
System.out.println("Authentication is currently working"); //this did print
Intent toMainActivity = new Intent( getBaseContext(), MainActivity.class);
startActivity(toMainActivity);
} else {
System.out.println("Failed authentication");
}
}
});
AuthData authData = user_data.getAuth();
if (authData != null) {
System.out.println("The state is: " + authData); //this did print
} else {
System.out.println("Failed");
}
In order for authentication sessions to be persisted across application restarts, you'll need to initialize the Firebase Android client library with you Android context:
From https://www.firebase.com/docs/android/guide/setup.html:
The Firebase library must be initialized once with an Android context.
This must happen before any Firebase reference is created or used. You
can add the Firebase setup code to your Android Application's or
Activity's onCreate method.
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
// other setup code
}
The Firebase client will automatically be authenticated on subsequent application cold starts. To check authentication state, see Firebase Android: Monitoring Authentication.

Android Facebook Share not showing description on facebook wall

When my app try to share something, the description is shown in the share activity, but it does not show up when it is posted. I went through many stackoverflow posts, but nothing could solve my problem.
Here is my call flow :
Share button is clicked
Activity calls a static method and passes itself and content to share to it via Bundle
Share activity is invoked from this static method. It displays the content to share correctly with all image, caption and description
Content is shared successfully
When the facebook post is checked, it just shows the playstore app details, with the image that I had set in 3, without the description
Here is the code that i use
if (FacebookDialog.canPresentShareDialog(activity.getApplicationContext(),
FacebookDialog.ShareDialogFeature.SHARE_DIALOG))
{
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(activity)
.setLink("<playstore link>")
.setApplicationName("<appname>")
.setName("<some name>")
.setCaption("<some text>")
.setDescription("a description")
.setPicture("http://url/to/image.jpg")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
}
But sharing the same using FeedDialog works
Bundle params = new Bundle();
params.putString("name", "name");
params.putString("caption", "caption");
params.putString("description","desc");
params.putString("link", "playstore link");
params.putString("picture", "http://url/to/image.jpg");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(activity,
session,
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(activity,
"Posted Successfully!",
Toast.LENGTH_SHORT).show();
activity.finish();
} else {
// User clicked the Cancel button
Toast.makeText(activity.getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
activity.finish();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(activity.getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
activity.finish();
} else {
Toast.makeText(activity.getApplicationContext(),
"An Error Occurred",
Toast.LENGTH_SHORT).show();
activity.finish();
}
}
})
.build();
feedDialog.show();
Thank You
From the docs (Versions 4.0+)
To show the ShareDialog for a link in your activity, create a
ShareDialog instance in your onCreate method:
public class MainActivity extends FragmentActivity {
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() { ... });
}
Then show the ShareDialog:
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(
"The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent);
}
Slartibartfast's Answer is working fine until compile 'com.facebook.android:facebook-android-sdk:4.24.0'
From Facebook SDK version 4.25.0
setContentTitle("") setContentDescription("") is depricated.
You have to use setQuote(""); Method instead of setContentDescription("");
For more detail about depreciation Refer: https://developers.facebook.com/docs/sharing/android

Categories

Resources