从Android发布视频到Facebook,深度链接回到应用程序

时间:2016-09-21 17:37:14

标签: android facebook deep-linking facebook-share deeplink

问题:我正在尝试通过托管在其他地方的Android应用与Facebook分享内容,并将其深层链接回我的应用。

详情 我正在开发和Android应用程序和我面临的问题是我有一个视频,我想分享给用户的新闻源与深度链接回应用程序。该视频托管在Cloudinary上,但这是无关紧要的,尝试将Youtube视频发布给用户新闻源,但是当他们点击下面的说明时将其重新链接回应用程序将是同样的问题。

我使用Facebook的ShareLinkContentShareDialog非常接近。代码如下所示:

ShareLinkContent content = new ShareLinkContent.Builder()
                        .setContentUrl(Uri.parse("https://kt4rh.app.goo.gl/naxy")) /* Deeplink back into my app created using Firebase Dynamic Link */
                        .setImageUrl(Uri.parse({path to an image stored on clodinary}))
                        .build();
                ShareDialog shareDialog = new ShareDialog(getActivity());
                shareDialog.show(content);

这会显示一个图片,当用户点击它时,会有一个深层链接返回我的应用。但是,我想要做的是显示视频(或gif,或者没关系)而不是图像。

我尝试将视频下载到设备并使用ShareVideoContent,希望我可以设置localUrlcontentUrl(类似于我设置缩略图和contentUrl的方式)在上面的ShareLinkContent中,像这样:

Uri videoUri = Uri.fromFile(new File(localPathToVideo));
                ShareVideo shareVideo = new ShareVideo.Builder()
                        .setLocalUrl(videoUri)
                        .build();
                ShareVideoContent videoContent  = new ShareVideoContent.Builder()
                        .setVideo(shareVideo)
                        .setContentUrl(Uri.parse("https://kt4rh.app.goo.gl/naxy")) /* Deeplink back to app */
                        .build();
                ShareDialog shareDialog = new ShareDialog(getActivity());
                callbackManager = CallbackManager.Factory.create();
                shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
                    @Override
                    public void onSuccess(Sharer.Result result) {}

                    @Override
                    public void onCancel() {}

                    @Override
                    public void onError(FacebookException error) {
                        Log.e(LOG_TAG, error.getMessage());
                    }
                });
                if (shareDialog.canShow(videoContent)) {
                    shareDialog.show(videoContent);
                }

但我只是回复说“只应指定一个链接,照片和视频。”,这意味着我无法设置本地网址和内容网址。

所以现在我被卡住了。看起来这应该是一个相对常见的问题(分享托管在其他地方的内容,并将其深层链接到您的应用程序),但我无法弄清楚如何做到这一点。

2 个答案:

答案 0 :(得分:1)

通过为每个要共享的项目(在本例中为行程路线)创建一个html页面并将其托管在Firebase的云存储上来结束解决此问题。这是代码的样子

/**
 * In order to share gifs to facebook and have a animating gif show up, we need to construct an arbitrary HTML page that wraps the
 * gif image that we want to share. When the user clicks on the wrapped html page, it will forward to the real page we want it to show.
 *
 * Facebook uses custom meta properties to determine what image to show in the preview, which is why we need to wrap the actual gif with another image this way
 */
private static final String GIF_IMAGE = "<gif_image_here>";
private static final String REDIRECT_LINK = "<redirect_link_here>";

private static final String SHARE_HTML_PAGE = "<!DOCTYPE html><html>" +
        "<head>" +
        "<meta property=\"og:type\" content=\"video.other\" />" +
        "<meta property=\"og:url\" content=\"" + GIF_IMAGE + "\" />" +
        "<meta property=\"og:image\" content=\"" + GIF_IMAGE + "\" />" +
        "<meta http-equiv=\"refresh\" content=\"0; url=" + REDIRECT_LINK + "\" />" +
        "</head></html>";

public static void shareGifToFacebook(final Activity activity, final String gifUrl, final Trip trip, final ShareStatusListener shareStatusListener) {
    String htmlPageToShare = SHARE_HTML_PAGE.replaceAll(GIF_IMAGE, gifUrl).replaceAll(REDIRECT_LINK, DeeplinkUtils.getTripDeeplink(trip.getUid()));

    // Upload the html page somewhere accessible
    FirebaseStorageUploader.uploadShareHtml(TravellerManager.getInstance().getCurrentlyLoggedInUserUid(), htmlPageToShare, new FirebaseStorageUploader.UploadShareHtmlListener() {
        @Override
        public void onSuccess(String publicUrl) {
            Log.d("ShareUtils", "shareGifToFacebook() publicUrl to be shortened:  " + publicUrl);

            URLShortener.shortUrl(publicUrl, new URLShortener.LoadingCallback() {
                @Override
                public void startedLoading() {

                }

                @Override
                public void finishedLoading(@Nullable String shortUrl) {
                    Log.d("ShareUtils", "shareGifToFacebook() finishedLoading() shortenedUrl " + shortUrl);

                    ShareLinkContent content = new ShareLinkContent.Builder()
                            .setContentUrl(Uri.parse(shortUrl))
                            .setContentDescription(activity.getString(R.string.view_trip_on_wd))
                            .setContentTitle(trip.getName())
                            .build();
                    ShareDialog shareDialog = new ShareDialog(activity);
                    shareDialog.show(content);

                    if (shareStatusListener != null) {
                        shareStatusListener.onShared();
                    }
                }
            });
        }

        @Override
        public void onFailed() {
            Toaster.toastLong(activity.getString(R.string.general_error));
        }
    });
}

答案 1 :(得分:0)

我认为在Android上您无法同时设置contentUrl和视频。我遇到了类似的问题,但在删除.setContentUrl(...)后才有效。

您可以做的是将链接复制到剪贴板,并诱使用户将链接粘贴到其标题中(即:在打开ShareDialog之前显示弹出/警告)。

可能还有其他方法可以解决这个问题。如果是这样,我也很想知道。

希望这会有所帮助。