简体   繁体   中英

Open app from Facebook link

I'm developing an app that shares some information in facebook. For that I do this:

if (FacebookDialog.canPresentShareDialog(getApplicationContext(), 
            FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
    // Publish the post using the Share Dialog
        FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
        .setName(attraction.getName())
        .setLink("http://developer.neosperience.com/")   
        .setDescription(attraction.getDescription())
        .setRef(String.valueOf(id_attraction))
        .setPicture(pictureURLtoShare)
        .build();
        uiHelper.trackPendingDialogCall(shareDialog.present());
    }

My problem is I need to send a parameter (id_attraction), so when the post from facebook is clicked it will open my app and I receive that parameter. I thought that setRef would work but I receive null. This is how I receive the Intent:

AppLinkData appLinkData = AppLinkData.createFromActivity(this);
if (appLinkData != null) {
    Bundle arguments = appLinkData.getArgumentBundle();
    //appLinkData.
    if (arguments != null) {
        String targetUrl = arguments.getString("target_url");
        if (targetUrl != null) {
          Log.i("Activity FB", "Target URL: " + targetUrl);
        }
    }
}

I dont know if it's the correct way to do this but I found a workaround. I send the parameter attached to the url using '?' so everything on the right of ? will be ignored by the browser.

if (FacebookDialog.canPresentShareDialog(getApplicationContext(), 
            FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
    // Publish the post using the Share Dialog
        FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
        .setName(attraction.getName())
        .setLink("http://developer.neosperience.com/?"+id_attraction)   
        .setDescription(attraction.getDescription())
        .setPicture(pictureURLtoShare)
        .build();
        uiHelper.trackPendingDialogCall(shareDialog.present());
    }

and then I receive it like this:

AppLinkData appLinkData = AppLinkData.createFromActivity(this);
if (appLinkData != null) {
    Bundle arguments = appLinkData.getArgumentBundle();
    //appLinkData.
    if (arguments != null) {
        String targetUrl = arguments.getString("target_url");
        if (targetUrl != null) {
          String[] partOfUrl = targetUrl.split("\\?");
          id_attraction = partOfUrl[1];
          Log.i("Activity FB", "Target URL: " + targetUrl);
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM