Add Long links creation
This commit is contained in:
parent
18b9a25ed5
commit
b714b0a4d9
@ -6,15 +6,22 @@ import android.net.Uri;
|
|||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.facebook.react.bridge.ActivityEventListener;
|
import com.facebook.react.bridge.ActivityEventListener;
|
||||||
import com.facebook.react.bridge.Promise;
|
import com.facebook.react.bridge.Promise;
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
|
import com.facebook.react.bridge.ReadableMap;
|
||||||
|
|
||||||
|
import com.google.firebase.dynamiclinks.DynamicLink;
|
||||||
|
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks;
|
||||||
|
import com.google.firebase.dynamiclinks.ShortDynamicLink;
|
||||||
|
import com.google.firebase.dynamiclinks.PendingDynamicLinkData;
|
||||||
|
|
||||||
import com.google.android.gms.tasks.OnFailureListener;
|
import com.google.android.gms.tasks.OnFailureListener;
|
||||||
import com.google.android.gms.tasks.OnSuccessListener;
|
import com.google.android.gms.tasks.OnSuccessListener;
|
||||||
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks;
|
|
||||||
import com.google.firebase.dynamiclinks.PendingDynamicLinkData;
|
|
||||||
|
|
||||||
import io.invertase.firebase.Utils;
|
import io.invertase.firebase.Utils;
|
||||||
|
|
||||||
@ -78,4 +85,167 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
|||||||
public void onNewIntent(Intent intent) {
|
public void onNewIntent(Intent intent) {
|
||||||
// TODO: Do I need to re-register the links handler for each new intent?
|
// TODO: Do I need to re-register the links handler for each new intent?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void createLink(final ReadableMap parameters, final Promise promise) {
|
||||||
|
try {
|
||||||
|
DynamicLink link = buildDynamicLinkFromMap(parameters);
|
||||||
|
|
||||||
|
promise.resolve(link.getUri().toString());
|
||||||
|
Log.d(TAG,link.getUri().toString());
|
||||||
|
}
|
||||||
|
catch(Exception ex) {
|
||||||
|
promise.reject("Could not build dynamic link", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a RN ReadableMap into a DynamicLink instance
|
||||||
|
*
|
||||||
|
* @param parameters
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private DynamicLink buildDynamicLinkFromMap(ReadableMap parameters) {
|
||||||
|
DynamicLink.Builder parametersBuilder = FirebaseDynamicLinks.getInstance().createDynamicLink();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(parameters);
|
||||||
|
|
||||||
|
parametersBuilder.setLink(Uri.parse((String)m.get("link")));
|
||||||
|
parametersBuilder.setDynamicLinkDomain((String)m.get("domain"));
|
||||||
|
|
||||||
|
setAndroidParameters(m, parametersBuilder);
|
||||||
|
setIosParameters(m, parametersBuilder);
|
||||||
|
//setNavigationInfoParameters(m, parametersBuilder);
|
||||||
|
setSocialMetaTagParameters(m, parametersBuilder);
|
||||||
|
setGoogleAnalyticsParameters(m, parametersBuilder);
|
||||||
|
setItunesConnectAnalyticsParameters(m, parametersBuilder);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "error while building parameters " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return parametersBuilder.buildDynamicLink();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAndroidParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
Map<String, Object> androidParameters = (Map<String, Object>) m.get("androidParameters");
|
||||||
|
if (androidParameters != null) {
|
||||||
|
DynamicLink.AndroidParameters.Builder androidParametersBuilder =
|
||||||
|
androidParameters.containsKey("packageName") ?
|
||||||
|
new DynamicLink.AndroidParameters.Builder((String)androidParameters.get("packageName")) :
|
||||||
|
new DynamicLink.AndroidParameters.Builder();
|
||||||
|
|
||||||
|
if (androidParameters.containsKey("fallbackUrl")) {
|
||||||
|
androidParametersBuilder.setFallbackUrl(Uri.parse((String)androidParameters.get("fallbackUrl")));
|
||||||
|
}
|
||||||
|
if (androidParameters.containsKey("minimumVersion")) {
|
||||||
|
androidParametersBuilder.setMinimumVersion(((Double)androidParameters.get("minimumVersion")).intValue());
|
||||||
|
}
|
||||||
|
parametersBuilder.setAndroidParameters(androidParametersBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setIosParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
Map<String, Object> iosParameters = (Map<String, Object>) m.get("iosParameters");
|
||||||
|
//TODO: see what happens if bundleId is missing
|
||||||
|
if (iosParameters != null && iosParameters.containsKey("bundleId")) {
|
||||||
|
DynamicLink.IosParameters.Builder iosParametersBuilder = new DynamicLink.IosParameters.Builder((String)iosParameters.get("bundleId"));
|
||||||
|
if (iosParameters.containsKey("appStoreId")) {
|
||||||
|
iosParametersBuilder.setAppStoreId((String)iosParameters.get("appStoreId"));
|
||||||
|
}
|
||||||
|
if (iosParameters.containsKey("customScheme")) {
|
||||||
|
iosParametersBuilder.setCustomScheme((String)iosParameters.get("customScheme"));
|
||||||
|
}
|
||||||
|
if (iosParameters.containsKey("fallbackUrl")) {
|
||||||
|
iosParametersBuilder.setFallbackUrl(Uri.parse((String)iosParameters.get("fallbackUrl")));
|
||||||
|
}
|
||||||
|
if (iosParameters.containsKey("ipadBundleId")) {
|
||||||
|
iosParametersBuilder.setIpadBundleId((String)iosParameters.get("ipadBundleId"));
|
||||||
|
}
|
||||||
|
if (iosParameters.containsKey("ipadFallbackUrl")) {
|
||||||
|
iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String)iosParameters.get("ipadFallbackUrl")));
|
||||||
|
}
|
||||||
|
if (iosParameters.containsKey("minimumVersion")) {
|
||||||
|
iosParametersBuilder.setMinimumVersion((String)iosParameters.get("minimumVersion"));
|
||||||
|
}
|
||||||
|
parametersBuilder.setIosParameters(iosParametersBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// private void setNavigationInfoParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
// Map<String, Object> navigationInfoParameters = (Map<String, Object>) m.get("navigationInfoParameters");
|
||||||
|
// if (navigationInfoParameters != null) {
|
||||||
|
// DynamicLink.NavigationInfoParameters.Builder navigationInfoParametersBuilder =
|
||||||
|
// new DynamicLink.NavigationInfoParameters.Builder();
|
||||||
|
//
|
||||||
|
// if (navigationInfoParameters.containsKey("forcedRedirectEnabled")) {
|
||||||
|
// navigationInfoParametersBuilder.setForcedRedirectEnabled((boolean)navigationInfoParameters.get("forcedRedirectEnabled"));
|
||||||
|
// }
|
||||||
|
// parametersBuilder.setNavigationInfoParameters(navigationInfoParametersBuilder.build());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
private void setSocialMetaTagParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
Map<String, Object> socialMetaTagParameters = (Map<String, Object>) m.get("socialMetaTagParameters");
|
||||||
|
if (socialMetaTagParameters != null) {
|
||||||
|
DynamicLink.SocialMetaTagParameters.Builder socialMetaTagParametersBuilder =
|
||||||
|
new DynamicLink.SocialMetaTagParameters.Builder();
|
||||||
|
|
||||||
|
if (socialMetaTagParameters.containsKey("description")) {
|
||||||
|
socialMetaTagParametersBuilder.setDescription((String)socialMetaTagParameters.get("description"));
|
||||||
|
}
|
||||||
|
if (socialMetaTagParameters.containsKey("imageUrl")) {
|
||||||
|
socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String)socialMetaTagParameters.get("imageUrl")));
|
||||||
|
}
|
||||||
|
if (socialMetaTagParameters.containsKey("title")) {
|
||||||
|
socialMetaTagParametersBuilder.setTitle((String)socialMetaTagParameters.get("title"));
|
||||||
|
}
|
||||||
|
parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setGoogleAnalyticsParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
Map<String, Object> googleAnalyticsParameters = (Map<String, Object>) m.get("googleAnalyticsParameters");
|
||||||
|
if (googleAnalyticsParameters != null) {
|
||||||
|
DynamicLink.GoogleAnalyticsParameters.Builder googleAnalyticsParametersBuilder =
|
||||||
|
new DynamicLink.GoogleAnalyticsParameters.Builder();
|
||||||
|
|
||||||
|
if (googleAnalyticsParameters.containsKey("campaign")) {
|
||||||
|
googleAnalyticsParametersBuilder.setCampaign((String)googleAnalyticsParameters.get("campaign"));
|
||||||
|
}
|
||||||
|
if (googleAnalyticsParameters.containsKey("content")) {
|
||||||
|
googleAnalyticsParametersBuilder.setContent((String)googleAnalyticsParameters.get("content"));
|
||||||
|
}
|
||||||
|
if (googleAnalyticsParameters.containsKey("medium")) {
|
||||||
|
googleAnalyticsParametersBuilder.setMedium((String)googleAnalyticsParameters.get("medium"));
|
||||||
|
}
|
||||||
|
if (googleAnalyticsParameters.containsKey("source")) {
|
||||||
|
googleAnalyticsParametersBuilder.setSource((String)googleAnalyticsParameters.get("source"));
|
||||||
|
}
|
||||||
|
if (googleAnalyticsParameters.containsKey("term")) {
|
||||||
|
googleAnalyticsParametersBuilder.setTerm((String)googleAnalyticsParameters.get("term"));
|
||||||
|
}
|
||||||
|
parametersBuilder.setGoogleAnalyticsParameters(googleAnalyticsParametersBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setItunesConnectAnalyticsParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
|
Map<String, Object> itunesConnectAnalyticsParameters = (Map<String, Object>) m.get("itunesConnectAnalyticsParameters");
|
||||||
|
if (itunesConnectAnalyticsParameters != null) {
|
||||||
|
DynamicLink.ItunesConnectAnalyticsParameters.Builder itunesConnectAnalyticsParametersBuilder =
|
||||||
|
new DynamicLink.ItunesConnectAnalyticsParameters.Builder();
|
||||||
|
|
||||||
|
if (itunesConnectAnalyticsParameters.containsKey("affiliateToken")) {
|
||||||
|
itunesConnectAnalyticsParametersBuilder.setAffiliateToken((String)itunesConnectAnalyticsParameters.get("affiliateToken"));
|
||||||
|
}
|
||||||
|
if (itunesConnectAnalyticsParameters.containsKey("campaignToken")) {
|
||||||
|
itunesConnectAnalyticsParametersBuilder.setCampaignToken((String)itunesConnectAnalyticsParameters.get("campaignToken"));
|
||||||
|
}
|
||||||
|
if (itunesConnectAnalyticsParameters.containsKey("providerToken")) {
|
||||||
|
itunesConnectAnalyticsParametersBuilder.setProviderToken((String)itunesConnectAnalyticsParameters.get("providerToken"));
|
||||||
|
}
|
||||||
|
parametersBuilder.setItunesConnectAnalyticsParameters(itunesConnectAnalyticsParametersBuilder.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,29 @@
|
|||||||
import { NativeEventEmitter, NativeModules } from 'react-native';
|
import { NativeEventEmitter, NativeModules } from 'react-native'
|
||||||
import { Base } from './../base';
|
import { Base } from './../base'
|
||||||
import { nativeSDKMissing } from './../../utils';
|
import { nativeSDKMissing } from './../../utils'
|
||||||
|
|
||||||
const FirebaseLinks = NativeModules.RNFirebaseLinks;
|
const FirebaseLinks = NativeModules.RNFirebaseLinks
|
||||||
const FirebaseLinksEvt = FirebaseLinks && new NativeEventEmitter(FirebaseLinks);
|
const FirebaseLinksEvt = FirebaseLinks && new NativeEventEmitter(FirebaseLinks)
|
||||||
|
|
||||||
const EVENT_TYPE = {
|
const EVENT_TYPE = {
|
||||||
Link: 'dynamic_link_received',
|
Link: 'dynamic_link_received',
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Links
|
* @class Links
|
||||||
*/
|
*/
|
||||||
export default class Links extends Base {
|
export default class Links extends Base {
|
||||||
constructor(firebase, options = {}) {
|
constructor(firebase, options = {}) {
|
||||||
super(firebase, options);
|
super(firebase, options)
|
||||||
if (!FirebaseLinks) {
|
if (!FirebaseLinks) {
|
||||||
return nativeSDKMissing('links');
|
return nativeSDKMissing('links')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.namespace = 'firebase:links';
|
this.namespace = 'firebase:links'
|
||||||
}
|
}
|
||||||
|
|
||||||
get EVENT_TYPE() {
|
get EVENT_TYPE() {
|
||||||
return EVENT_TYPE;
|
return EVENT_TYPE
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,7 +31,7 @@ export default class Links extends Base {
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
getInitialLink() {
|
getInitialLink() {
|
||||||
return FirebaseLinks.getInitialLink();
|
return FirebaseLinks.getInitialLink()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,11 +40,19 @@ export default class Links extends Base {
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
onLink(listener: Function): () => any {
|
onLink(listener: Function): () => any {
|
||||||
const rnListener = FirebaseLinksEvt.addListener(EVENT_TYPE.Link, listener);
|
const rnListener = FirebaseLinksEvt.addListener(EVENT_TYPE.Link, listener)
|
||||||
return () => rnListener.remove();
|
return () => rnListener.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createLink(parameters: Object = {}): Promise<String> {
|
||||||
|
return FirebaseLinks.createLink(parameters)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildShortDynamicLink(link: string, domain: string): Promise {
|
||||||
|
// return FirebaseLinks.buildShortDynamicLink(link, domain)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const statics = {
|
export const statics = {
|
||||||
EVENT_TYPE,
|
EVENT_TYPE,
|
||||||
};
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user