如何在 Ionic 4 中使用 cordova firebase.dynamiclinks 插件?

How do I use cordova firebase.dynamiclinks plugin in Ionic 4?

我想在我的 Ionic 4 应用程序中使用 Cordova Firebase Dynamiclinks 插件:https://github.com/chemerisuk/cordova-plugin-firebase-dynamiclinks#installation

也有一个 Ionic-native-plugin 用法:npm install @ionic-native/firebase-dynamic-links 和用法:

import { FirebaseDynamicLinks } from '@ionic-native/firebase-dynamic-links/ngx';

constructor(private firebaseDynamicLinks: FirebaseDynamicLinks) { }

...
this.firebaseDynamicLinks.onDynamicLink()
  .subscribe((res: any) => console.log(res), (error:any) => console.log(error));

问题是:我想使用 Cordova Firebase Dynamiclinks 插件中可用的 createDynamicLink(parameters) 方法,但 Ionic-native-plugin

Property 'createDynamicLink' does not exist on type 'FirebaseDynamicLinks'.

因此,我需要直接使用 Cordova Firebase Dynamiclinks,我尝试像

这样使用它
import { cordova } from '@ionic-native/core';
...
cordova.plugins.firebase.dynamiclinks.createDynamicLink({
    link: "https://google.com"
}).then(function(url) {
    console.log("Dynamic link was created:", url);
}); 

但是出现错误

Property 'plugins' does not exist on type '(pluginObj: any, methodName: string, config: CordovaOptions, args: IArguments | any[]) => any'.

还尝试删除导入

cordova.plugins.firebase.dynamiclinks.createDynamicLink({
    link: "https://google.com"
}).then(function(url) {
    console.log("Dynamic link was created:", url);
});

得到这个

Property 'firebase' does not exist on type 'CordovaPlugins'.

cordova 插件的正确用法是什么?

更新

ionic-native-plugin 现在包含 Cordova Firebase Dynamiclinks 插件中可用的所有方法。

我认为这更适合作为评论,但我还没有获得足够的声誉。

目前,@ionic-team/ionic-native 仓库中有一个开放的 PR(here). This exposes the extra methods, but until then you can use the original repo here to get your desired effect. In order to install the repo you will have to follow the directions in the Developer guide here。干杯!

我开发了一个使用 Firebase 动态链接的 ionic 5 应用程序,它运行良好,但需要一些努力。我观看了视频以了解 Firebase 动态链接的工作原理,但肯定有很多内容没有显示。

要回答原始问题,您可以随时 manually create a dynamic link 这就是我在我们的解决方案中所做的。我们创建了一个动态 link 来帮助用户入职(注册一个帐户)。我们的动态 link 具有来自后端进程的自定义 onboardingId,并且 link 通过 SMS 文本消息呈现给用户。

这是在app.component.ts构造函数中 这是当用户单击动态 link:

时发生的一些代码
// Handle the logic here after opening the app (app is already installed) with the Dynamic link
this.firebaseDynamicLinks.onDynamicLink().subscribe((res: any) =>  {
  console.log('app was opened with dynamic link');
  console.log(res);
  /*   This only fires on subsequent calls and not on app start 20220208 STR
  console.log(JSON.stringify(res)); //"{"deepLink":"https://localhost/onboard?onboardingId=8ed634b0-53b7-4a0f-b67e-12c06019982a","clickTimestamp":1643908387670,"minimumAppVersion":0}"
  var dynamicLink = JSON.parse(JSON.stringify(res));
  var deepLink = dynamicLink.deepLink;
  console.log(deepLink);
  if (deepLink.indexOf("onboard")>=0){
    this.isOnboarding = true;
  }
  alert("deepLink ="+ deepLink);
  */
}, (error:any) => {
  console.log(error)
});

我最初认为如果用户没有安装应用程序,Firebase 会处理所有的魔法。我错了!安装应用程序后,您还必须处理代码以获取动态 link。

下面的代码将从剪贴板中读取动态 link 并在应用安装过程中保留下来。放置在 app.component.ts ngOnInit().

this.platform.ready().then(() => {

  this.firebaseDynamicLinks.getDynamicLink().then((data) => {
    //added 20220208 STR try to help open the deep link if app is just installed
    if (data != null) {
      console.log(JSON.stringify(data));
      //alert("initializeApp():"+JSON.stringify(data));
      var dynamicLink = JSON.parse(JSON.stringify(data));
      var deepLink = dynamicLink.deepLink;
      console.log("initializeApp():"+deepLink);
      if (deepLink != "") {
        if (deepLink.indexOf("onboard")>=0){
          this.isOnboarding = true;
          this.deepLinkToOnboard(deepLink);
        }
      }
    }
  });}

所以要在安装 Firebase 插件后处理动态 links,您必须有两段代码;一个用于处理应用程序是否已安装,另一个用于处理动态 link 如果应用程序未安装。