完成华为厂商通道插件
This commit is contained in:
commit
83d63cdfac
31
README.md
Normal file
31
README.md
Normal 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
24
package.json
Normal 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
23
plugin.xml
Normal 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
72
scripts/install.js
Normal 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
36
scripts/uninstall.js
Normal 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'));
|
||||
}
|
||||
};
|
7
src/android/build-tpns-hms.gradle
Normal file
7
src/android/build-tpns-hms.gradle
Normal 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
7
www/TpnsHmsPlugin.js
Normal file
@ -0,0 +1,7 @@
|
||||
var TpnsHmsPlugin = function () { };
|
||||
|
||||
TpnsHmsPlugin.prototype.hi =function (success) {
|
||||
(success||console.log)('hi hms ready.');
|
||||
};
|
||||
|
||||
module.exports = new TpnsHmsPlugin();
|
Loading…
Reference in New Issue
Block a user