如何使用Firebase动态链接在特定位置打开我的应用程序?

时间:2019-07-01 22:05:55

标签: android flutter dart deep-linking firebase-dynamic-links

我正在使用 Flutter 开发一个应用,该应用需要共享指向不同文章页面的链接。如何使用 Firebase动态链接 来做到这一点?

我进行了分享活动。现在,我正在努力使用 firebase_dynamic_link 生成的链接自动打开应用程序。该链接仅将我重定向到Play商店,但无其他地方。我浏览过文档和有关媒体的一些文章。我尝试了他们显示的所有步骤,但是什么也没有。我什至尝试用空字符串替换url字符串,以获取正确的应用路径示例“ / location” 。我也已在Play商店上发布了该应用程序。因此,如果有人已经这样做了。请给我一个示例文件或仓库以检查或只是解决方案谢谢

这是main.dart文件的代码:

import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:piautobox/screens/add_article_screen.dart';
import 'package:piautobox/screens/article_details_screen.dart';
import 'package:piautobox/screens/chossen_service_screen.dart';
import 'package:piautobox/screens/company_details_screen.dart';
import 'package:piautobox/screens/company_profile_screen.dart';
import 'package:piautobox/screens/edit_article_screen.dart';
import 'package:piautobox/screens/image_hero_screen.dart';
import 'package:piautobox/screens/login_screen.dart';
import 'package:piautobox/screens/my_articles_list_screen.dart';
import 'package:piautobox/screens/password_forget_screen.dart';
import 'package:piautobox/screens/private_screen.dart';
import 'package:piautobox/screens/registration_screen.dart';
import 'package:piautobox/screens/splash_screen.dart';
import 'package:piautobox/screens/subscription_screen.dart';
import 'package:piautobox/screens/user_details_screen.dart';
import 'package:piautobox/screens/welcome_screen.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';

void main() {
  runApp(PiAutoBox());
}

class PiAutoBox extends StatefulWidget {
  PiAutoBox({Key key}) : super(key: key);

  _PiAutoBoxState createState() => _PiAutoBoxState();
}

class _PiAutoBoxState extends State<PiAutoBox> with WidgetsBindingObserver {
  Timer _timerLink;

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    if (_timerLink != null) {
      _timerLink.cancel();
    }
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      _timerLink = new Timer(const Duration(milliseconds: 1000), () {
        _retrieveDynamicLink();
      });
    }
  }

  Future<void> _retrieveDynamicLink() async {
    final PendingDynamicLinkData data =
        await FirebaseDynamicLinks.instance.retrieveDynamicLink();
    final Uri deepLink = data?.link;
    print('ici');
    if (deepLink != null) {

      Navigator.pushNamed(context, deepLink.path);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      color: Theme.of(context).primaryColor,
      title: 'PiAutoBox',
      theme: ThemeData(
        primaryColor: Color(0xffffe415),
        fontFamily: 'Lato',
        textTheme: TextTheme(
          body1: TextStyle(fontSize: 15),
        ),
      ),
      home: StreamBuilder(
        stream: FirebaseAuth.instance.onAuthStateChanged,
        builder: (ctx, AsyncSnapshot<FirebaseUser> snap) {
          if (snap.connectionState == ConnectionState.waiting) {
            return SplashScreen();
          }

          if (snap.hasData) {
            return PrivateScreen();
          }

          return WelcomeScreen();
        },
      ),
      routes: {
        //auth screens
        '/login': (BuildContext context) => LoginScreen(),
        '/register': (BuildContext context) => RegistrationScreen(),
        '/passwordForget': (BuildContext context) => PasswordForgetScreen(),

        //account links
        '/private': (BuildContext context) => PrivateScreen(),
        '/userDetails': (BuildContext context) => UserDetailsScreen(),
        '/subscriptions': (BuildContext context) => SubsscriptionScreen(),
        '/companyDetails': (BuildContext context) => CompanyDetailsScreen(),
        '/choosenServices': (BuildContext context) => ChoosenServiceScreen(),
        '/myArticleList': (BuildContext context) => MyArticlesListScreen(),
        '/editArticle': (BuildContext context) => EditArticleScreen(),
        '/articleDetails': (BuildContext context) => ArticleDetailsScreen(),
        '/companyProfile': (BuildContext context) => CompanyProfileScreen(),
        '/addArticle': (BuildContext context) => AddArticleScreen(),
        '/imageHero': (BuildContext context) => ImageHeroScreen(),
      },
    );
  }
}

这是生成共享链接的功能

Future<Uri> getDynamicLink([String path = 'companyProfile']) async {
  final DynamicLinkParameters parameters = DynamicLinkParameters(
    uriPrefix: 'https://piautobox.page.link',
    link: Uri.parse('https://piautobox-expo.firebaseapp.com/$path'),
    androidParameters: AndroidParameters(
      packageName: 'com.apptailors.piautobox',
      minimumVersion: 20,
    ),
  );

  final uri = await parameters.buildUrl();

  final ShortDynamicLink shortenedLink = await DynamicLinkParameters.shortenUrl(
    uri,
    DynamicLinkParametersOptions(
        shortDynamicLinkPathLength: ShortDynamicLinkPathLength.short),
  );

  return shortenedLink.shortUrl;
}

0 个答案:

没有答案