Ionic 3:从 PlayStore 获取价值 link

Ionic 3: Getting value from PlayStore link

我有一个 Ionic 3 应用程序,我想根据从 Playstore link 下载的内容在其中设置一些变量。 例如,http://linktoplaystore.com/app?account=4 会将我的应用程序中的帐户变量设置为 4。有什么办法可以实现吗?

您可以通过解析和定义数组在代码中完成此操作

private urlParameters: Array<any> = [];

if (YOURURLVARIABLE.indexOf("?") > 0) {
    let splitURL = document.URL.split("?");
    let splitParams = splitURL[1].split("&");
    let i: any;
    for (i in splitParams){
        let singleURLParam = splitParams[i].split('=');
        let urlParameter = {
        'name': singleURLParam[0],
        'value': singleURLParam[1]
    };
    this.urlParameters.push(urlParameter);
    }
}

this.urlParamID = navParams.get('account').value;

我认为这在 Playstore 中是不可能的,但您可以通过深层链接从您自己的外部来源访问应用程序功能。有关详细信息,请参阅 https://ionicframework.com/docs/native/deeplinks/

this.deeplinks.route({
    '/account/:accountId': AccountPage
}).subscribe(match => {
    console.log(match.$args);
}, nomatch => {
    console.error('Got a deeplink that didn\'t match', nomatch);
});

解析JavaScript中的link得到需要的值

代码段

var link = "http://linktoplaystore.com/app?account=4";
var variable = link.split('?')[1].split('=')[1];

Also, you should open an API to send this link from server, so you don't need to push app-update in case of any changes in the link.

希望对您有所帮助!

实现此目的的唯一方法是 Firebase 动态链接(如我之前的评论所述)。

动态链接可以在应用程序安装过程中保留下来。因此,您可以使用参数。但是链接必须在某处创建(手动或代码)。

看看https://firebase.google.com/docs/dynamic-links/. The Ionic plugin for this is still in beta though: https://ionicframework.com/docs/native/firebase-dynamic-links/

我终于设法使用 Firebase 动态链接使其工作。

在 Firebase 控制台中设置我的应用程序后,我手动创建了一个 link,如下所示:

https://myapp.page.link/?link=https://play.google.com/store/apps/details?id=com.ionicframework.myapp?account4

在我的 Ionic 应用程序中,我已经安装并配置了 Firebase Dynamic Links 插件:https://ionicframework.com/docs/native/firebase-dynamic-links/

然后,在app.component.ts里面,平台初始化后,我使用这段代码:

firebaseDynamicLinks.onDynamicLink().subscribe((res: any) => {
 let link = res;
}, err => {
  alert(err);
});

link 将是一个 JavaScript 对象,如下所示:

{"deepLink":"https://play.google.com/store/apps/details?id=com.ionicframework.couriermanagerclient1234?account4","clickTimestamp":1535449992657,"minimumAppVersion":0}

从这里,我可以解析结果并从 deepLink 属性

中提取帐户参数