Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 25, 2015

Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?

Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


I have added sharing functionality to Android app as described here https://developers.facebook.com/docs/android/share-dialog/#setup

But I have noticed that if user is cancelled sharing activity onComplete is called anyway

    @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);        uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {          @Override          public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {              Log.e("Activity", String.format("Error: %s", error.toString()));          }            @Override          public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {              Log.e("Activity", "Success!");          }      });  }  

I have also looked in to Bundle which is returned. Even if I cancel share dialog i get

com.facebook.platform.extra.DID_COMPLETE=true  

How can I get result that user really shared data on facebook? (Without making separate login with facebook button. Maybe some permissions need to be added?)

Answer by Ming Li for Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


See https://developers.facebook.com/docs/android/share-dialog/#handling-responses

You can tell if the user has cancelled by calling

String gesture = FacebookDialog.getNativeDialogCompletionGesture(data);  if (gesture != null) {    if ("post".equals(gesture)) {      // the user hit Post    } else if ("cancel".equals(gesture)) {      // the user hit cancel    } else {      // unknown value    }  } else {    // either an error occurred, or your app has never been authorized  }  

where data is the result bundle. However, it will only return a non-null value IF the user has logged in via your app (i.e. you have at least basic_info permissions). If the user has never logged in or authorized your app, then the only thing you'll see is the DID_COMPLETE, and it will always be true unless an error occurred. This is by design.

Answer by JHNeves for Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


In order to obtain the result for the sharing, your app needs to have at least the basic_info permission.

To solve that, just open an session (this will automatically request the basic_info permission):

Session.openActiveSession(this /*your activity*/,                             true /*allows the UI login to show up if needed*/,                             new Session.StatusCallback() {      @Override      public void call(Session session, SessionState state, Exception exception) {          Log.i("[Facebook]", "Session: " + state.toString());          if (session.isOpened()) {             /// now you are good to get the sharing results          }      }  });  

You can find more information in here: https://developers.facebook.com/docs/android/getting-started/

Answer by Rony Tesler for Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


  @Override          public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {              if (data.getString("com.facebook.platform.extra.COMPLETION_GESTURE").equals("cancel"))                  return;  }  

the value of data.getString("com.facebook.platform.extra.COMPLETION_GESTURE") is "post" when the user did post on Facebook.

Answer by Himanshu Narang for Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


Use this code:-

/**   * Facebook Dialog Callback    *    * Called up when come back from Share Dialog   *    */  private class FacebookDialogCallBack implements FacebookDialog.Callback  {      @Override      public void onComplete(PendingCall pendingCall, Bundle data)       {          //Show Toast Message          showToastMessage(data);      }        @Override      public void onError(PendingCall pendingCall, Exception error, Bundle data)       {          //Show Toast Message          showToastMessage(data);      }  }  //Show Toast Message  private void showToastMessage(Bundle data)  {      //Get Native Dialog Did Complete      boolean didComplete = FacebookDialog.getNativeDialogDidComplete(data);        if(didComplete)      {          //Get Native Dialog Completion Gesture          String nativeDialogCompletionGesture = FacebookDialog.getNativeDialogCompletionGesture(data);            if (nativeDialogCompletionGesture == null || FacebookDialog.COMPLETION_GESTURE_CANCEL.equals(nativeDialogCompletionGesture))          {              //Show Publish Cancel Toast              UIUtil.showToast(R.string.toast_message_share_publish_cancelled);                                                         }          else          {              //Show Success Post Toast              UIUtil.showToast(R.string.toast_message_share_success_post);          }      }      else      {          //Show Publish Cancel Toast          UIUtil.showToast(R.string.toast_message_share_publish_cancelled);      }  }  

Answer by Change for Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?


Heading ## private static ShareDialog shareDialog;

    private static FacebookCallback shareCallback = new FacebookCallback() {          @Override          public void onCancel() {              Log.d("HelloFacebook", "Canceled");          }            @Override          public void onError(FacebookException error) {              Log.d("HelloFacebook", String.format("Error: %s", error.toString()));              String title = this.getActivty().getString(R.string.error);              String alertMessage = error.getMessage();              showResult(title, alertMessage);          }            @Override          public void onSuccess(Sharer.Result result) {              Log.d("HelloFacebook", "Success!");              // ?????????              if (result.getPostId() != null) {                  String title = this.getActivty().getString(R.string.success);                  String id = result.getPostId();                  String alertMessage = this.getActivty().getString(R.string.successfully_posted_post, id);                  showResult(title, alertMessage);              }          }            private void showResult(String title, String alertMessage) {              new AlertDialog.Builder(this.getActivty())                      .setTitle(title)                      .setMessage(alertMessage)                      .setPositiveButton(R.string.ok, null)                      .show();          }      };    protected void onCreate (Bundle savedInstanceState)  {               shareDialog = new ShareDialog(this. getActivty());               shareDialog.registerCallback( callbackManager, shareCallback);  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.