Hi,
we have a weird situation with branch.subscribe().
The scenario - the app is opened from deeplink, we get a push notifications (shows modal inside the app) with new deeplink, it opens the browser, navigates back to the app (really quickly) and subscribe not called.
When we minimize (not close) the app again and then opening it, it suddently detects the link that was supposed to open (even though we just opened the app from the apps tray) and then we get the correct callback.
How to solve this? is skipCahcedEvents related? we tried with and without.
Are we adding the events wrong? we call useDeepLinking in App.tsx, the root component.
We are using react native cli, not expo, version 0.61.5.
This is the subscribe call:
const useDeepLinking = () => {
let _unsubscribe = useRef<PlainFunction>(null).current;
useAppStateChanged({
// active app state
onForeground: () => {
_unsubscribe = (branch.subscribe(params => {
const { error, params: data } = params;
console.log('CALLED INSIDE BRANCH', params);
if (data['+clicked_branch_link']) {
redirectHandler(data);
}
}) as unknown) as PlainFunction;
},
// background app state
onBackground: () => {
if (_unsubscribe && typeof _unsubscribe === 'function') {
console.log('REMOVED BRANCH SUBSCRIBE');
_unsubscribe();
_unsubscribe = null;
}
}
});
};
This is the useAppStateChanged hook:
interface IParams {
onForeground: PlainFunction;
onBackground: PlainFunction;
}
const useAppStateChanged = (params: IParams) => {
useEffect(() => {
const eventsMapper = {
background: params.onBackground,
active: params.onForeground,
inactive: () => {}
};
const handleAppStateChange = (nextAppState: AppStateStatus) => {
const cb = eventsMapper[nextAppState];
if (cb && typeof cb === 'function') {
eventsMapper[nextAppState]();
}
};
AppState.addEventListener('change', handleAppStateChange);
return () => AppState.removeEventListener('change', handleAppStateChange);
}, []);
};
@SasonBraha
We just released a new SDK that resolves a long list of issues. Could you verify if this still occurs?
Also is it on both Android and iOS? Thank you.
@echo-branch
I can verify that this issue still exists on iOS. In fact, It looks like the subscribe method is totally busted and never gets triggered anymore.
We're running RN 61.5 + react-native-branch 4.3.0 + Branch pod version 0.31.3
@Cal-L
Are you in the exact same scenario as the original report with push notifications? Or is your setup different and encountering a similar issue?
If your setup is different could you provide some more details about the application architecture?
@echo-branch
While we're not using the subscribe method, I'm encountering the issue where it's never being triggered on iOS.
Repro: Clicks deep link -> Opens app store -> Downloads the app -> Open the app -> Branch.subscribe is never triggered
Even when we kill the app and open it back up, branch subscribe never triggers.
In our use case, we use the getLatestReferringParams in an async function. Our issue is that getLatestReferringParams doesn't detect any deep link info on a fresh install (follow same steps as the top). However, as soon as you background/foreground the app, getLatestReferringParams returns the correct deep link info. (Similar behavior as the OP)
The implementation is pretty straight forward. We just call this whenever we need to:
async checkBranchDeeplink(): Promise<IBranchLinkInfo> {
await delayFunctionThatWaitsAFewSeconds(); // Branch may take a few seconds to register data
let deepLink = await Branch.getLatestReferringParams(false);
return deepLink;
}
App delegate:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if (![RNBranch.branch application:app openURL:url options:options]) {
// do other deep link routing for the Facebook SDK, Pinterest SDK, etc
}
return YES;
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
return [RNBranch continueUserActivity:userActivity];
}
// Deprecated for newer versions
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (![RNBranch.branch application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) {
// do other deep link routing for the Facebook SDK, Pinterest SDK, etc
}
return YES;
}
@Cal-L Thanks for the additional information. I'll try to get some eyes on this in our next sprint, which starts next week.
@echo-branch
Ah I found the my issue, it was a silly mistake. In the AppDelegate: [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:true] was missing! After adding that back, both issues were resolved on our end. (Subscribe gets called and getLatestReferringParams receives the deep link data from a fresh install).
@echo-branch , I'm still seeing what seems to be exactly that same problem on Android.
If I open a branch link while my application is open, the subscribe callback is called correctly. But if the application is closed, and I try to open the link, the app opens but subscribe is never called. Curiously, if I put the app in background then get back to it from the tray, the subscribe callback is called with the parameters from the previous link click.
I'm using RN 0.61.5 + react-native-branch 4.4.0
I'm also having problems with iOS - the app opens after a click on a link, but the subscribe callback is simply never called. I reviewed the integration steps multiple times with no success. Also double-checked to see if I was missing initSessionWithLaunchOptions (as did @Cal-L ), but everything looks OK.
Do you have any ideas of why this is happening?
I simplified as much as I could to make sure we were not making any mistakes on the integration.
On JS I've got this on my index.js
import { AppRegistry } from 'react-native';
import Branch from 'react-native-branch';
import App from './src/app';
import { name as appName } from './app.json';
Branch.initSessionTtl = 10000;
Branch.subscribe(params => {
console.log('branch callback called with params!', params);
});
AppRegistry.registerComponent(appName, () => App);
Then MainActivity.java and MainApplication.java:
@Override
protected void onStart() {
super.onStart();
RNBranchModule.setDebug();
RNBranchModule.initSession(getIntent().getData(), this);
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
RNBranchModule.reInitSession(this);
}
public void onCreate() {
...
super.onCreate();
RNBranchModule.getAutoInstance(this);
...
}
Hi @guiccbr. Sorry you're still having trouble. It sounds like you may have more than one issue. If you haven't already, could you please open a ticket with [email protected]?
Your code looks fine, for the most part, though ordinarily users destructure the arguments to the subscribe callback:
Branch.subscribe({ params, error } => {
if (error) {
console.log('error from Branch: ' + error);
return;
}
console.log(params from Branch: ' + params);
});
So I guess in your case you'll get params.params and params.error. It might be necessary to review your project. Maybe it's easier for you to submit fuller code snippets privately to support. At worst, I could do a screenshare with you and find out what's going on.
still an issue for me on Android
For anyone experiencing this problem on Android in combination with React Native Navigation please be aware that on the initial load you might need to use a different flow. It will call getInitialURL where you might want to handle the branch data using branch.getLatestReferringParams() instead of in the subscribe function.
The subscribe might be called too late/spacing out for some other reason.
Check out this example for what this might look like: https://help.branch.io/developers-hub/docs/react-native#read-deep-link
Most helpful comment
still an issue for me on Android