处理 Flutter 应用程序中的动态链接以转到应用程序中的特定页面
Handling dynamic links in Flutter app to go to specific pages within app
我在 Firebase 控制台中创建了一个动态 link 并在 Flutter 中添加了代码以将我带到我的应用程序中的特定页面。这行得通。我现在不确定如何在 Firebase 控制台中创建新的动态 link 后如何在 Flutter 中创建更多动态 link 以将我带到应用程序中的 more/other 页面。我遵循了教程中的以下代码:
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Dynamic Links Example',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => MyApp(),
'/helloworld': (BuildContext context) => LinkPage(),
},
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _linkMessage;
bool _isCreatingLink = false;
String _testString =
"To test: long press link and then copy and click from a non-browser "
"app. Make sure this isn't being tested on iOS simulator and iOS xcode "
"is properly setup. Look at firebase_dynamic_links/README.md for more "
"details.";
@override
void initState() {
super.initState();
initDynamicLinks(); //
}
void setupNotification() async {
_firebaseMessaging.getToken().then((token) {
print(token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("Message: $message");
},
onResume: (Map<String, dynamic> message) async {
print("Message: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("Message: $message");
},
);
}
void initDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
print(deepLink);
if (deepLink != null) {
Navigator.pushNamed(context, '/helloworld');
}
}, onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
});
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
Navigator.pushNamed(context, '/helloworld');
}
}
Future<void> _createDynamicLink(bool short) async {
setState(() {
_isCreatingLink = true;
});
final DynamicLinkParameters parameters = DynamicLinkParameters(
uriPrefix: 'http://cx4k7.app.goo.gl',
link: Uri.parse('My http url'),
androidParameters: AndroidParameters(
packageName: 'my package id/name',
minimumVersion: 0,
),
dynamicLinkParametersOptions: DynamicLinkParametersOptions(
shortDynamicLinkPathLength: ShortDynamicLinkPathLength.short,
),
);
Uri url;
if (short) {
final ShortDynamicLink shortLink = await parameters.buildShortLink();
url = shortLink.shortUrl;
} else {
url = await parameters.buildUrl();
}
setState(() {
_linkMessage = url.toString();
_isCreatingLink = false;
});
}
如果 deeplink 不为空会发生什么的 if 语句是找到答案的关键。因此,只需编写一些 if 语句来说明当 deeplink 具有与 FCM 控制台相同的值时会发生什么
我在 Firebase 控制台中创建了一个动态 link 并在 Flutter 中添加了代码以将我带到我的应用程序中的特定页面。这行得通。我现在不确定如何在 Firebase 控制台中创建新的动态 link 后如何在 Flutter 中创建更多动态 link 以将我带到应用程序中的 more/other 页面。我遵循了教程中的以下代码:
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Dynamic Links Example',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => MyApp(),
'/helloworld': (BuildContext context) => LinkPage(),
},
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _linkMessage;
bool _isCreatingLink = false;
String _testString =
"To test: long press link and then copy and click from a non-browser "
"app. Make sure this isn't being tested on iOS simulator and iOS xcode "
"is properly setup. Look at firebase_dynamic_links/README.md for more "
"details.";
@override
void initState() {
super.initState();
initDynamicLinks(); //
}
void setupNotification() async {
_firebaseMessaging.getToken().then((token) {
print(token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("Message: $message");
},
onResume: (Map<String, dynamic> message) async {
print("Message: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("Message: $message");
},
);
}
void initDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
print(deepLink);
if (deepLink != null) {
Navigator.pushNamed(context, '/helloworld');
}
}, onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
});
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
Navigator.pushNamed(context, '/helloworld');
}
}
Future<void> _createDynamicLink(bool short) async {
setState(() {
_isCreatingLink = true;
});
final DynamicLinkParameters parameters = DynamicLinkParameters(
uriPrefix: 'http://cx4k7.app.goo.gl',
link: Uri.parse('My http url'),
androidParameters: AndroidParameters(
packageName: 'my package id/name',
minimumVersion: 0,
),
dynamicLinkParametersOptions: DynamicLinkParametersOptions(
shortDynamicLinkPathLength: ShortDynamicLinkPathLength.short,
),
);
Uri url;
if (short) {
final ShortDynamicLink shortLink = await parameters.buildShortLink();
url = shortLink.shortUrl;
} else {
url = await parameters.buildUrl();
}
setState(() {
_linkMessage = url.toString();
_isCreatingLink = false;
});
}
如果 deeplink 不为空会发生什么的 if 语句是找到答案的关键。因此,只需编写一些 if 语句来说明当 deeplink 具有与 FCM 控制台相同的值时会发生什么