I've been trying to set up deeplinking to my app via Firebase Dynamic Links and have used inputs from the following SO questions here and here.
However, my app has Firebase Auth, so the way I have implemented identifying if there is a deeplink is by scanning in the 'Home' class (post auth) and then basing my code on the examples and from this Medium article
The issue is that the deeplink/dynamic link works in that the app is launched, but the data passed as parameters - the PendingDynamicLink data is null, and thus the parameters aren't picked up.
What am I missing here - any help would be much appreciated!
Future<Uri> createDynamicLink({@required String docId}) async {
final DynamicLinkParameters parameters = DynamicLinkParameters(
uriPrefix: 'https://reviv.page.link',
link: Uri.parse('https://reviv.page.link/doctor?docid=$docId'),
dynamicLinkParametersOptions: DynamicLinkParametersOptions(
shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
androidParameters: AndroidParameters(
packageName: 'co.reviv.reviv_user.debug',
minimumVersion: 0,
),
iosParameters: IosParameters(
bundleId: 'co.reviv.reviv_user.debug',
minimumVersion: '1',
appStoreId: '',
),
);
final link = await parameters.buildUrl();
final ShortDynamicLink shortenedLink =
await DynamicLinkParameters.shortenUrl(
link,
DynamicLinkParametersOptions(
shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
);
print("deeplink : " + shortenedLink.shortUrl.toString());
return shortenedLink.shortUrl;
}
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(builder: (context) => LoadingIndicatorHelper()),
ChangeNotifierProvider(builder: (context) => RevivUser()),
],
child: CareProviderApp(),
));
}
class CareProviderApp extends StatelessWidget {
final ThemeData _revivTheme = _buildRevivTheme();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Reviv Provider App",
theme: _revivTheme,
routes: <String, WidgetBuilder>{
"/": (BuildContext context) => RootPage(auth: Auth()),
"/home": (BuildContext context) =>
MyHomePage(title: "Reviv Home", patient: null),
"/login": (BuildContext context) => Login(title: "Reviv Sign In"),
"/signup": (BuildContext context) =>
SignupScreen(title: "Reviv Sign Up")
},
initialRoute: "/", //new rootpage.RootPage(auth: Auth())
);
}
}
class MyHomePage extends StatefulWidget {
Patient patient;
MyHomePage({Key key, this.title, @required this.patient}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
Timer _timerLink;
List<Widget> _children;
void initState() {
super.initState();
initDynamicLinks();
// ... other stuff goes here
WidgetsBinding.instance.addObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_timerLink = new Timer(const Duration(milliseconds: 1000), () {
initDynamicLinks();
});
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
if (_timerLink != null) {
_timerLink.cancel();
}
super.dispose();
}
void initDynamicLinks() async {
print("checking for deeplinks");
FirebaseDynamicLinks.instance
.getInitialLink()
.then((PendingDynamicLinkData data) {
if (data == null) print("PendingDynamicLink data is null");
final Uri deepLink = data?.link;
if (deepLink != null) {
print("found deeplink");
Doctor doctor;
final queryParams = deepLink.queryParameters;
if (queryParams.length > 0) {
String doctorId = queryParams["docid"];
print("Received deeplink pointing to doctor id: $doctorId");
_docService.getDoctor(doctorId).then((Doctor _doctor) {
doctor = _doctor;
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
DoctorDetail(doctor, widget.patient)));
});
}
} else {
print("no deeplink");
}
}
@override
Widget build(BuildContext context) {
// .. yada yada
}
Here my output is always "PendingDynamicLink data is null". What am I getting wrong?
Checking the code snippet you've shared, it seems that you're fetching the Dynamic Link data inside MyHomePage. Calling FirebaseDynamicLinks.getInitialLink
to fetch PendingDynamicLinkData
should be run on main() to be called during app start as mentioned on this setup guide.
Furthermore, there was an announcement that Firebase Dynamic Links will be shut down at some point in the future. So maybe you'd like to plan a migration soon.