完成华为厂商通道插件

This commit is contained in:
范大德 2023-08-03 21:21:14 +08:00
commit 83d63cdfac
7 changed files with 200 additions and 0 deletions

31
README.md Normal file
View File

@ -0,0 +1,31 @@
title: TpnsHms
description: 腾讯移动推送华为厂商通道插件
***
# cordova-plugin-tpns-hms
腾讯移动推送插件
## Installation
```bash
#安装前请从华为推送平台获取的应用配置文件 agconnect-services.json 到项目根目录
cordova plugin add cordova-plugin-tpns-hms
```
### Params
## Supported Platforms
* Huawei
## Get Started
### Usage
##### 1.从华为推送平台获取的应用配置文件 agconnect-services.json 到项目根目录
##### 2.安装本插件

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "cordova-plugin-tpns-hms",
"version": "0.0.1",
"description": "",
"main": "index.js",
"cordova": {
"id": "cordova-plugin-tpns-hms",
"platforms": [
"android"
]
},
"repository": {
"type": "git",
"url": "https://m.shuto.cn:8681/public/cordova-plugin-tpns-hms.git"
},
"keywords": [
"cordova",
"tpns",
"android",
"push",
"hms"
],
"author": "fandd@shuto.cn"
}

23
plugin.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-tpns-hms" version="0.0.1"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>TpnsHmsPlugin</name>
<dependency id="cordova-plugin-tpns" url="https://m.shuto.cn:8681/public/cordova-plugin-tpns.git" version="0.0.1"/>
<js-module name="TpnsHmsPlugin" src="www/TpnsHmsPlugin.js">
<clobbers target="TpnsHms" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="TpnsHmsPlugin">
<param name="android-package" value="com.shuto.plugin.tpns.hms.TpnsHmsPlugin" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml"></config-file>
<framework src="src/android/build-tpns-hms.gradle" custom="true" type="gradleReference" />
<hook type="before_plugin_install" src="scripts/install.js" />
<hook type="before_plugin_add" src="scripts/install.js" />
<hook type="before_prepare" src="scripts/install.js"/>
<hook type="before_plugin_uninstall" src="scripts/uninstall.js" />
</platform>
</plugin>

72
scripts/install.js Normal file
View File

@ -0,0 +1,72 @@
/**
* ~ Copyright (c) 2023 Beijing Shuto Technology Co,. Ltd. All rights reserved.
*/
const path = require("path");
const fs = require("fs");
/**
* 读取文件内容然后替换部分内容后写回
* @param file
* @param callback
*/
function replaceContents(file, callback) {
var data = fs.readFileSync(file, 'utf8');
data = callback(data);
fs.writeFileSync(file, data, 'utf8');
}
/**
* 一些自定义操作
* @param ctx
*/
module.exports = function (ctx) {
console.log('============install huawei message service hook=================');
var rootDir = ctx.opts.projectRoot;
var project = path.join(rootDir, 'platforms/android');
var app = path.join(rootDir, 'platforms/android/app');
if(!fs.existsSync(path.join(rootDir, 'agconnect-services.json'))){
throw new Error('请从华为推送平台获取的应用配置文件 agconnect-services.json 到项目根目录!');
}
if(fs.existsSync(path.join(app, 'agconnect-services.json'))){
console.log("已经添加hms配置,跳过");
return;
}
fs.copyFileSync(path.join(rootDir, 'agconnect-services.json'),path.join(app, 'agconnect-services.json'));
//1. 添加华为 maven 仓库地址 buildscript allprojects repositories
//1.1 如果repositories.gradle文件存在,则添加相关配置
var repositoriesFiles = [
path.join(rootDir, "repositories.gradle"),
path.join(project, "repositories.gradle"),
path.join(app, "repositories.gradle")
];
for (const repositoriesFile of repositoriesFiles) {
if (fs.existsSync(repositoriesFile)) {
replaceContents(repositoriesFile, (data) => {
var data = data.replace(/ext\.repos = \{/g, "ext.repos = {\n maven {url 'https://developer.huawei.com/repo/'}");
return data;
});
}
}
//1.2 在所有的gradle文件中替换相关配置
var repository = data => data.replace(/repositories \{/g,"repositories {\n maven {url 'https://developer.huawei.com/repo/'}");
fs.readdirSync(project).forEach(function (name) {
var filePath = path.join(project, name);
var stat = fs.statSync(filePath);
if (stat.isFile() && name.endsWith('.gradle')) {
replaceContents(filePath, repository);
} else if (stat.isDirectory()) {
fs.readdirSync(filePath).forEach(function (subName) {
var subFilePath = path.join(filePath, subName);
var subStat = fs.statSync(subFilePath);
if (subStat.isFile() && subName.endsWith('.gradle')) {
replaceContents(subFilePath, repository);
}
});
}
});
//2. 添加华为推送 gradle 插件依赖
replaceContents(path.join(project,'build.gradle'), data => data.replace(/dependencies \{/g, "dependencies {\n classpath 'com.huawei.agconnect:agcp:1.6.0.300'"));
};

36
scripts/uninstall.js Normal file
View File

@ -0,0 +1,36 @@
/**
* ~ Copyright (c) 2023 Beijing Shuto Technology Co,. Ltd. All rights reserved.
*/
const path = require("path");
const fs = require("fs");
/**
* 读取文件内容然后替换部分内容后写回
* @param file
* @param callback
*/
function replaceContents(file, callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
data = callback(data);
fs.writeFile(file, data, 'utf8', writeErr => {
if (err) throw err;
console.log(file, 'replace success.');
});
});
}
/**
* 一些自定义操作
* @param ctx
*/
module.exports = function (ctx) {
console.log('============uninstall huawei message service hook=================');
var rootDir = ctx.opts.projectRoot;
var project = path.join(rootDir, 'platforms/android');
var app = path.join(rootDir, 'platforms/android/app');
if(fs.existsSync(path.join(app, 'agconnect-services.json'))){
fs.rmSync(path.join(app, 'agconnect-services.json'));
}
};

View File

@ -0,0 +1,7 @@
apply plugin: 'com.huawei.agconnect'
dependencies {
// TPNS [VERSION] SDK 2
implementation 'com.tencent.tpns:huawei:1.4.0.1-release' // [VERSION] SDK Android SDK
implementation 'com.huawei.hms:push:6.7.0.300'
}

7
www/TpnsHmsPlugin.js Normal file
View File

@ -0,0 +1,7 @@
var TpnsHmsPlugin = function () { };
TpnsHmsPlugin.prototype.hi =function (success) {
(success||console.log)('hi hms ready.');
};
module.exports = new TpnsHmsPlugin();