Reputation: 1837
I have two activities: In first activity there is Facebook Login button in which user can successfully login. In the next activity there is a button to share on the user wall. Now before sharing for the first time, it goes to take the publish permission from user. And when user allows, it comes back, but does not continue the share operation. But the next time I go to the same activity and press button to share,then it shares everytime.
So the issue is when for the first permission taken from the user it does not continue its sharing operation. I want it to work for the first time as well. Both the operations simultaneously user allows , and after that it goes for share instantly. Below is my code:
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
{
// Check for publish permissions
if (!hasPublishPermission())
{
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
ringProgressDialog = ProgressDialog.show(context, "Please wait ...", "Sharing on facebook ...", true);
Bundle postParams = new Bundle();
postParams.putString("name", "Test");
postParams.putString("message", "Test message");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
try
{
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(getClass().getSimpleName(), "Facebook JSON error " + e.getMessage());
} catch (Exception e) {
Log.i(getClass().getSimpleName(), "Facebook error " + e.getMessage());
}
ringProgressDialog.dismiss();
FacebookRequestError error = response.getError();
if (error != null) {
Utils.showToast_msg(context, error.getErrorMessage());
} else {
Utils.showToast_msg(context, "Shared Successfully");
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
///////////////////////////////////////////////////////////////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
Upvotes: 2
Views: 214
Reputation: 1312
In this block :
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
At the end, you're using return;
, so even when you successfully get the new permission, the code after the if
is not being called. It works the second time because this block is not executed, the app already having the permission.
An asynchronous way (reference) to do it would be: (Some function names might be off)
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
newPermissionsRequest.setCallback(new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
publishStory();
}
});
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Upvotes: 1