private void executeAppInvite(JSONArray args, CallbackContext callbackContext) { String appLinkUrl = null; String previewImageUrl = null; Map<String, String> params = new HashMap<String, String>(); String method = null; JSONObject parameters; try { parameters = args.getJSONObject(0); } catch (JSONException e) { parameters = new JSONObject(); } Iterator<String> iter = parameters.keys(); while (iter.hasNext()) { String key = iter.next(); if (key.equals("url")) { try { appLinkUrl = parameters.getString(key); } catch (JSONException e) { Log.w(TAG, "Nonstring method parameter provided to dialog"); callbackContext.error("Incorrect parameter 'url'."); return; } } else if (key.equals("picture")) { try { previewImageUrl = parameters.getString(key); } catch (JSONException e) { Log.w(TAG, "Non-string parameter provided to dialog discarded"); callbackContext.error("Incorrect parameter 'picture'."); return; } } } if (appLinkUrl == null || previewImageUrl == null) { callbackContext.error("Both 'url' and 'picture' parameter needed"); return; } if (AppInviteDialog.canShow()) { AppInviteContent content = new AppInviteContent.Builder() .setApplinkUrl(appLinkUrl) .setPreviewImageUrl(previewImageUrl) .build(); appInviteDialog.show(content); } }
@Override public boolean canShow(final AppInviteContent content, boolean isBestEffort) { return AppInviteDialog.canShowWebFallback(); }
@Override public boolean canShow(AppInviteContent content, boolean isBestEffort) { return AppInviteDialog.canShowNativeDialog(); }
@Override protected void pluginInitialize() { FacebookSdk.sdkInitialize(cordova.getActivity().getApplicationContext()); // create callbackManager callbackManager = CallbackManager.Factory.create(); // create AppEventsLogger logger = AppEventsLogger.newLogger(cordova.getActivity().getApplicationContext()); // Set up the activity result callback to this class cordova.setActivityResultCallback(this); LoginManager.getInstance() .registerCallback( callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(final LoginResult loginResult) { GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject jsonObject, GraphResponse response) { if (response.getError() != null) { if (graphContext != null) { graphContext.error( getFacebookRequestErrorResponse(response.getError())); } else if (loginContext != null) { loginContext.error( getFacebookRequestErrorResponse(response.getError())); } return; } // If this login comes after doing a new permission request // make the outstanding graph call if (graphContext != null) { makeGraphCall(); return; } Log.d(TAG, "returning login object " + jsonObject.toString()); loginContext.success(getResponse()); loginContext = null; } }) .executeAsync(); } @Override public void onCancel() { FacebookOperationCanceledException e = new FacebookOperationCanceledException(); handleError(e, loginContext); } @Override public void onError(FacebookException e) { Log.e("Activity", String.format("Error: %s", e.toString())); handleError(e, loginContext); } }); shareDialog = new ShareDialog(cordova.getActivity()); shareDialog.registerCallback( callbackManager, new FacebookCallback<Sharer.Result>() { @Override public void onSuccess(Sharer.Result result) { if (showDialogContext != null) { showDialogContext.success(result.getPostId()); showDialogContext = null; } } @Override public void onCancel() { FacebookOperationCanceledException e = new FacebookOperationCanceledException(); handleError(e, showDialogContext); } @Override public void onError(FacebookException e) { Log.e("Activity", String.format("Error: %s", e.toString())); handleError(e, showDialogContext); } }); messageDialog = new MessageDialog(cordova.getActivity()); messageDialog.registerCallback( callbackManager, new FacebookCallback<Sharer.Result>() { @Override public void onSuccess(Sharer.Result result) { if (showDialogContext != null) { showDialogContext.success(); showDialogContext = null; } } @Override public void onCancel() { FacebookOperationCanceledException e = new FacebookOperationCanceledException(); handleError(e, showDialogContext); } @Override public void onError(FacebookException e) { Log.e("Activity", String.format("Error: %s", e.toString())); handleError(e, showDialogContext); } }); gameRequestDialog = new GameRequestDialog(cordova.getActivity()); gameRequestDialog.registerCallback( callbackManager, new FacebookCallback<GameRequestDialog.Result>() { @Override public void onSuccess(GameRequestDialog.Result result) { if (showDialogContext != null) { try { JSONObject json = new JSONObject(); json.put("requestId", result.getRequestId()); json.put("recipientsIds", new JSONArray(result.getRequestRecipients())); showDialogContext.success(); showDialogContext = null; } catch (JSONException ex) { showDialogContext.success(); showDialogContext = null; } } } @Override public void onCancel() { FacebookOperationCanceledException e = new FacebookOperationCanceledException(); handleError(e, showDialogContext); } @Override public void onError(FacebookException e) { Log.e("Activity", String.format("Error: %s", e.toString())); handleError(e, showDialogContext); } }); appInviteDialog = new AppInviteDialog(cordova.getActivity()); appInviteDialog.registerCallback( callbackManager, new FacebookCallback<AppInviteDialog.Result>() { @Override public void onSuccess(AppInviteDialog.Result result) { if (showDialogContext != null) { showDialogContext.success(); showDialogContext = null; } } @Override public void onCancel() { FacebookOperationCanceledException e = new FacebookOperationCanceledException(); handleError(e, showDialogContext); } @Override public void onError(FacebookException e) { Log.e("Activity", String.format("Error: %s", e.toString())); handleError(e, showDialogContext); } }); }