From 83d63cdfac9893fb71aebf047c320977a3fde85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=A4=A7=E5=BE=B7?= Date: Thu, 3 Aug 2023 21:21:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8D=8E=E4=B8=BA=E5=8E=82?= =?UTF-8?q?=E5=95=86=E9=80=9A=E9=81=93=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 31 +++++++++++++ package.json | 24 +++++++++++ plugin.xml | 23 ++++++++++ scripts/install.js | 72 +++++++++++++++++++++++++++++++ scripts/uninstall.js | 36 ++++++++++++++++ src/android/build-tpns-hms.gradle | 7 +++ www/TpnsHmsPlugin.js | 7 +++ 7 files changed, 200 insertions(+) create mode 100644 README.md create mode 100644 package.json create mode 100644 plugin.xml create mode 100644 scripts/install.js create mode 100644 scripts/uninstall.js create mode 100644 src/android/build-tpns-hms.gradle create mode 100644 www/TpnsHmsPlugin.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..4891851 --- /dev/null +++ b/README.md @@ -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.安装本插件 diff --git a/package.json b/package.json new file mode 100644 index 0000000..89d335a --- /dev/null +++ b/package.json @@ -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" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..01b0fa1 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,23 @@ + + + TpnsHmsPlugin + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/scripts/install.js b/scripts/install.js new file mode 100644 index 0000000..35870a7 --- /dev/null +++ b/scripts/install.js @@ -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'")); +}; \ No newline at end of file diff --git a/scripts/uninstall.js b/scripts/uninstall.js new file mode 100644 index 0000000..14038b2 --- /dev/null +++ b/scripts/uninstall.js @@ -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')); + } +}; \ No newline at end of file diff --git a/src/android/build-tpns-hms.gradle b/src/android/build-tpns-hms.gradle new file mode 100644 index 0000000..1af0d54 --- /dev/null +++ b/src/android/build-tpns-hms.gradle @@ -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' +} + \ No newline at end of file diff --git a/www/TpnsHmsPlugin.js b/www/TpnsHmsPlugin.js new file mode 100644 index 0000000..0b9591a --- /dev/null +++ b/www/TpnsHmsPlugin.js @@ -0,0 +1,7 @@ +var TpnsHmsPlugin = function () { }; + +TpnsHmsPlugin.prototype.hi =function (success) { + (success||console.log)('hi hms ready.'); +}; + +module.exports = new TpnsHmsPlugin(); \ No newline at end of file