Search code examples
androidandroid-facebookshareactionprovider

Do any widely used Android apps have Facebook sharing with pre-populated text field?


I'm creating an Android app for a small business owner. I've implemented social sharing with a ShareActionProvider, and I have discovered that Facebook purposely does not allow apps to pre-populate posts. The user must enter their own text. (See this S.O. post and then this S.O. post ).

Multiple S.O. answers have indicated that by using the Facebook SDK, it is possible to achieve this result (pre-populating Facebook posts).

On the other hand, I learned that Facebook has [https://developers.facebook.com/docs/apps/review/prefill] (a policy) against doing this.

My questions are:
1) Is this really possible? If so, how difficult is it to implement compared to the SharActionProvider?

2) Are there any popular or major apps whose Facebook sharing feature pre-populates text for the user?

3) Has Facebook been known to crack down on this policy? What has been their action towards people who break this policy?

This is the first Android app I've created and the first project I've done in 'real life'. I'm trying to figure out if adding this feature would be worth it for his business. He indicated he would like this feature to create an easy way to users to share content.


Solution

  • In my app I share the quotes from various famous personalities to social media. I have integrated Facebook SDK to enable user share it on their wall. I have not faced any crackdown from Facebook. App is being used over by 5K people as of now.

    So to answer your questions,

    1. Yes, it is possible. Not difficult to implement

    2. I am not aware of other popular apps that use this feature

    3. No crackdown or action from FB

    This is what I do to share the quote on user's FB wall.

    public void onClickFBShare(View view) {
    		quotview = (TextView) findViewById(R.id.MainText);
    		line = quotview.getText().toString(); //converting the text to String
    		Intent FBIntent = new Intent("android.intent.action.SHAREHELPER");
    		FBIntent.putExtra("quote", line);
    		startActivity(FBIntent);
    	}

    The code to post the content is in Sharehelper.Java

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Arrays;
    import java.util.List;
    
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.content.pm.Signature;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Base64;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    import com.facebook.Request;
    import com.facebook.Response;
    import com.facebook.Session;
    import com.facebook.SessionState;
    import com.facebook.UiLifecycleHelper;
    import com.facebook.model.GraphUser;
    import com.facebook.widget.LoginButton;
    import com.facebook.widget.LoginButton.UserInfoChangedCallback;
    
    
    public class ShareHelper extends ActionBarActivity{
    
    	    private LoginButton loginBtn;
    	    private Button updateStatusBtn;
    	 
    	    private TextView fbquote;
    	    
    	    private TextView userName;
    	 
    	    private UiLifecycleHelper uiHelper;
    	 
    	    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
    	    
    	    private String message;
    	    
    	    public void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	        
    	     // Add code to print out the key hash
    	        try {
    	            PackageInfo info = getPackageManager().getPackageInfo(
    	                    "com.facebook.samples.hellofacebook", 
    	                    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 (NameNotFoundException e) {
    
    	        } catch (NoSuchAlgorithmException e) {
    
    	        }
    
    	        
    	        Intent intent = getIntent();
    		    
    		    message = intent.getStringExtra("quote");     
    		 	
    	 
    	        uiHelper = new UiLifecycleHelper(this, statusCallback);
    	        uiHelper.onCreate(savedInstanceState);
    	 
    	        setContentView(R.layout.sharehelper_activity);
    	    
    	        fbquote = (TextView)findViewById(R.id.FbTextView);
    	        fbquote.setText(message);
    	        
    	        userName = (TextView) findViewById(R.id.user_name);
    	        loginBtn = (LoginButton) findViewById(R.id.fb_login_button);
    	        loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
    	            @Override
    	            public void onUserInfoFetched(GraphUser user) {
    	                if (user != null) {
    	                    userName.setText("Hello, " + user.getName());
    	                } else {
    	                    userName.setText("You are not logged");
    	                }
    	            }
    	        });
    	 
    	        updateStatusBtn = (Button) findViewById(R.id.update_status);
    	        updateStatusBtn.setOnClickListener(new OnClickListener() {
    	 
    	            @Override
    	            public void onClick(View v) {
    	            	postStatusMessage();
    	            }
    	        });
    	 
    	        buttonsEnabled(false);
    	    }
    	 
    	    private Session.StatusCallback statusCallback = new Session.StatusCallback() {
    	        @Override
    	        public void call(Session session, SessionState state,
    	                Exception exception) {
    	            if (state.isOpened()) {
    	                buttonsEnabled(true);
    	                Log.d("FacebookSampleActivity", "Facebook session opened");
    	            } else if (state.isClosed()) {
    	                buttonsEnabled(false);
    	                Log.d("FacebookSampleActivity", "Facebook session closed");
    	            }
    	        }
    	    };
    	 
    	    public void buttonsEnabled(boolean isEnabled) {
    	        updateStatusBtn.setEnabled(isEnabled);
    	    }
    	 
    	   
    	    public void postStatusMessage() {
    	        if (checkPermissions()) {
    	            Request request = Request.newStatusUpdateRequest(
    	                    Session.getActiveSession(), message,
    	                    new Request.Callback() {
    	                        @Override
    	                        public void onCompleted(Response response) {
    	                            if (response.getError() == null)
    	                                Toast.makeText(ShareHelper.this,
    	                                        "Quote Shared successfully",
    	                                        Toast.LENGTH_LONG).show();
    	                        }
    	                    });
    	            request.executeAsync();
    	        } else {
    	            requestPermissions();
    	        }
    	    }
    	 
    	    public boolean checkPermissions() {
    	        Session s = Session.getActiveSession();
    	        if (s != null) {
    	            return s.getPermissions().contains("publish_actions");
    	        } else
    	            return false;
    	    }
    	 
    	    public void requestPermissions() {
    	        Session s = Session.getActiveSession();
    	        if (s != null)
    	            s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
    	                    this, PERMISSIONS));
    	    }
    	 
    	    @Override
    	    public void onResume() {
    	        super.onResume();
    	        uiHelper.onResume();
    	        buttonsEnabled(Session.getActiveSession().isOpened());
    	    }
    	 
    	    @Override
    	    public void onPause() {
    	        super.onPause();
    	        uiHelper.onPause();
    	    }
    	 
    	    @Override
    	    public void onDestroy() {
    	        super.onDestroy();
    	        uiHelper.onDestroy();
    	    }
    	 
    	    @Override
    	    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	        super.onActivityResult(requestCode, resultCode, data);
    	        uiHelper.onActivityResult(requestCode, resultCode, data);
    	    }
    	 
    	    @Override
    	    public void onSaveInstanceState(Bundle savedState) {
    	        super.onSaveInstanceState(savedState);
    	        uiHelper.onSaveInstanceState(savedState);
    	    }
    }