mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[links] Start work on DynamicLinks support
This commit is contained in:
@@ -61,4 +61,5 @@ dependencies {
|
||||
compile "com.google.firebase:firebase-config:$firebaseVersion"
|
||||
compile "com.google.firebase:firebase-perf:$firebaseVersion"
|
||||
compile "com.google.firebase:firebase-ads:$firebaseVersion"
|
||||
compile "com.google.firebase:firebase-invites:$firebaseVersion"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package io.invertase.firebase.links;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.facebook.react.bridge.ActivityEventListener;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.google.android.gms.tasks.OnFailureListener;
|
||||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks;
|
||||
import com.google.firebase.dynamiclinks.PendingDynamicLinkData;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
|
||||
public class RNFirebaseLinks extends ReactContextBaseJavaModule implements ActivityEventListener {
|
||||
private final static String TAG = RNFirebaseLinks.class.getCanonicalName();
|
||||
private String initialLink = null;
|
||||
|
||||
public RNFirebaseLinks(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
getReactApplicationContext().addActivityEventListener(this);
|
||||
registerLinksHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RNFirebaseLinks";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getInitialLink(Promise promise) {
|
||||
promise.resolve(initialLink);
|
||||
}
|
||||
|
||||
private void registerLinksHandler() {
|
||||
Activity activity = getCurrentActivity();
|
||||
if (activity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
FirebaseDynamicLinks.getInstance()
|
||||
.getDynamicLink(activity.getIntent())
|
||||
.addOnSuccessListener(activity, new OnSuccessListener<PendingDynamicLinkData>() {
|
||||
@Override
|
||||
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
|
||||
// Get deep link from result (may be null if no link is found)
|
||||
if (pendingDynamicLinkData != null) {
|
||||
Uri deepLinkUri = pendingDynamicLinkData.getLink();
|
||||
String deepLink = deepLinkUri.toString();
|
||||
// TODO: Validate that this is called when opening from a deep link
|
||||
if (initialLink == null) {
|
||||
initialLink = deepLink;
|
||||
}
|
||||
Utils.sendEvent(getReactApplicationContext(), "dynamic_link_received", deepLink);
|
||||
}
|
||||
}
|
||||
})
|
||||
.addOnFailureListener(activity, new OnFailureListener() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Exception e) {
|
||||
Log.w(TAG, "getDynamicLink:onFailure", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
|
||||
// Not required for this module
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewIntent(Intent intent) {
|
||||
// TODO: Do I need to re-register the links handler for each new intent?
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.invertase.firebase.links;
|
||||
|
||||
import com.facebook.react.ReactPackage;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.uimanager.UIManagerModule;
|
||||
import com.facebook.react.uimanager.ViewManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class RNFirebaseLinksPackage implements ReactPackage {
|
||||
public RNFirebaseLinksPackage() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reactContext react application context that can be used to create modules
|
||||
* @return list of native modules to register with the newly created catalyst instance
|
||||
*/
|
||||
@Override
|
||||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||
List<NativeModule> modules = new ArrayList<>();
|
||||
modules.add(new RNFirebaseLinks(reactContext));
|
||||
|
||||
return modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list of JS modules to register with the newly created catalyst instance.
|
||||
* <p/>
|
||||
* IMPORTANT: Note that only modules that needs to be accessible from the native code should be
|
||||
* listed here. Also listing a native module here doesn't imply that the JS implementation of it
|
||||
* will be automatically included in the JS bundle.
|
||||
*/
|
||||
@Override
|
||||
public List<Class<? extends JavaScriptModule>> createJSModules() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reactContext
|
||||
* @return a list of view managers that should be registered with {@link UIManagerModule}
|
||||
*/
|
||||
@Override
|
||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user