diff --git a/.gitignore b/.gitignore index 804f047..2fb02eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ bin/ +build/ obj/ .vscode/ .vs/ diff --git a/README.md b/README.md index bd85032..85beaf5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # HMS Core Push Kit Sample Code (C#) -English | [中文](README_ZH.md) +English | [中文](./README_ZH.md) ## Contents * [Introduction](#Introduction) * [Installation](#Installation) @@ -23,26 +23,45 @@ The following table describes main classes used in the sample code. | Class Name| Description| | ----------- | ----------- | |AppOptions|App-related configuration.| +|ApiVersion|Push Kit API version.| |AGConnectApp|App.| |AGConnectMessaging|Push Kit API calling methods.| |Message|Message body.| ## Installation +### Source code + 1. Decompress the sample code. 2. Copy **AGConnectAdmin** to a proper position in your Visual Studio solution and reference the corresponding assembly in your project. 3. Use the classes in **AGConnectAdmin** by referring to the sample code. +### Local NuGet + +1. Download NuGet package from Releases +2. Create *Resources* directory and move downloaded package there +3. Create **nuget.config** file at solution directory and paste +``` + + + + + + +``` +4. Install package from local NuGet to target project + ## Configuration The following table describes the parameters related to the **AppOptions** class. | Parameter| Description| | ----------- | ----------- | +|ProjectId|Project ID, which is obtained from the project information.| |ClientId|App ID, which is obtained from the app information.| |ClientSecret|App secret, which is obtained from the app information.| |LoginUri|URL for Huawei OAuth 2.0 to obtain a token. For details, please refer to [OAuth 2.0-based Authentication](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/oauth2-0000001212610981).| -|ApiBaseUri|Access address of Push Kit. For details, please refer to [Downlink Message Sending](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-server-dev-0000001050040110?ha_source=hms1).| +|ApiVersion|Push Kit API version. For details, please refer to [Sending Downlink Messages](https://developer.huawei.com/consumer/en/doc/HMSCore-References/https-send-api-0000001050986197).| ## Environment Requirements @@ -50,6 +69,7 @@ The demo projects need to be opened using Visual Studio 2017 or a later version. - .NET Framework 4.5 or later - .NET Standard 2.0 or later +- .NET 6.0 or later ## Sample Code diff --git a/README_ZH.md b/README_ZH.md index 93ae4f3..20db989 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1,5 +1,5 @@ # 华为推送服务服务端C#示例代码 -[English](README.md) | 中文 +[English](./README.md) | 中文 ## 目录 * [简介](#简介) @@ -24,26 +24,45 @@ C#示例代码对华为推送服务(HUAWEI Push Kit)服务端接口进行封 | 类名 | 说明 | | ----------- | ----------- | |AppOptions|应用相关配置| +|ApiVersion|推送服务API版本。| |AGConnectApp|应用| |AGConnectMessaging|推送服务相关接口的调用方法| |Message|消息体| ## 安装 +### 源代码 + 1. 解压示例代码。 2. 将解压后的AGConnectAdmin复制到你的Visual Studio Solution中适当的位置,在你的应用工程中引用对应的程序集即可。 3. 参考示例代码来使用AGConnectAdmin中的类。 +### 本地 NuGet + +1. 从 Releases 下载 NuGet 包 +2. 创建 *Resources* 目录并将下载的包移动到那里 +3. 在解决方案目录中创建 **nuget.config** 文件并粘贴 +``` + + + + + + +``` +4. 从本地 NuGet 安装包到目标项目 + ## 配置 以下描述了AppOptions类的相关参数。 | 参数 | 说明 | | ----------- | ----------- | +|ProjectId|项目ID,从项目信息中获取。| |ClientId|应用ID,从应用信息中获取| |ClientSecret|应用访问密钥,从应用信息中获取| |LoginUri|华为OAuth 2.0获取token的地址。详情请参见[基于OAuth 2.0开放鉴权-客户端模式](https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/oauth2-0000001212610981#section128682386159?ha_source=hms1)。| -|ApiBaseUri|推送服务的访问地址。详情请参见[推送服务-下行消息](https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/android-server-dev-0000001050040110?ha_source=hms1)。| +|ApiVersion|推送服务API版本。 详情请参见【下行消息发送】(https://developer.huawei.com/consumer/cn/doc/HMSCore-References/https-send-api-0000001050986197)。| ## 环境要求 @@ -51,6 +70,7 @@ C#示例代码对华为推送服务(HUAWEI Push Kit)服务端接口进行封 - .NET Framework 4.5以上 - .NET Standard 2.0以上 +- .NET 6.0以上 ## 示例代码 diff --git a/src/.gitattributes b/src/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/src/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..8f74558 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,367 @@ +# Custom (user-defined) +build/ +build/* + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/src/AGConnectAdmin.Examples/AGConnectAdmin.Examples.csproj b/src/AGConnectAdmin.Examples/AGConnectAdmin.Examples.csproj index ddefa7d..fc365f3 100644 --- a/src/AGConnectAdmin.Examples/AGConnectAdmin.Examples.csproj +++ b/src/AGConnectAdmin.Examples/AGConnectAdmin.Examples.csproj @@ -1,7 +1,8 @@  - netcoreapp3.0 + net6.0 + ../build/$(MSBuildProjectName)/ @@ -11,9 +12,11 @@ - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/AGConnectAdmin.Examples/Example.cs b/src/AGConnectAdmin.Examples/Example.cs index 1668b85..34cf610 100644 --- a/src/AGConnectAdmin.Examples/Example.cs +++ b/src/AGConnectAdmin.Examples/Example.cs @@ -19,6 +19,8 @@ namespace AGConnectAdmin.Examples { public partial class Example { + private const ApiVersion _apiVersion = ApiVersion.V1; + #region Tokens private const string TOKEN_IOS = "your ios token"; private const string TOKEN_ANDROID = "your android token"; @@ -33,6 +35,8 @@ public Example(ITestOutputHelper logger) AGConnectApp.Create(new AppOptions() { + ApiVersion = _apiVersion, + ProjectId = "your project id", ClientId = "your client id", ClientSecret = "your cliient secret", }); diff --git a/src/AGConnectAdmin.Tests/AGConnectAdmin.Tests.csproj b/src/AGConnectAdmin.Tests/AGConnectAdmin.Tests.csproj index 9167419..16bb021 100644 --- a/src/AGConnectAdmin.Tests/AGConnectAdmin.Tests.csproj +++ b/src/AGConnectAdmin.Tests/AGConnectAdmin.Tests.csproj @@ -1,19 +1,23 @@  - netcoreapp3.0 + net6.0 false true ../AGConnectAdmin.snk + + ../build/$(MSBuildProjectName)/ - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/AGConnectAdmin.Tests/TestUtils.cs b/src/AGConnectAdmin.Tests/TestUtils.cs index 8313e63..badb6b2 100644 --- a/src/AGConnectAdmin.Tests/TestUtils.cs +++ b/src/AGConnectAdmin.Tests/TestUtils.cs @@ -30,7 +30,7 @@ public static void GlobalInit() { Initialized = true; AGConnectApp.Create(TestUtils.ReadOptionsFromDisk()); - } + } } public static AppOptions ReadOptionsFromDisk() @@ -40,7 +40,8 @@ public static AppOptions ReadOptionsFromDisk() return new AppOptions() { LoginUri = keyValuePairs["login_uri"], - ApiBaseUri = keyValuePairs["api_base_uri"], + ApiVersion = ApiVersion.V2, + ProjectId = keyValuePairs["dev_proj_id"], ClientId = keyValuePairs["dev_app_id"], ClientSecret = keyValuePairs["client_secret"] }; diff --git a/src/AGConnectAdmin.Tests/options.json b/src/AGConnectAdmin.Tests/options.json index 76905d5..e4c87cd 100644 --- a/src/AGConnectAdmin.Tests/options.json +++ b/src/AGConnectAdmin.Tests/options.json @@ -1,6 +1,6 @@ { - "login_uri": "https://login.cloud.huawei.com/oauth2/v2/token", - "api_base_uri": "https://push-api.cloud.huawei.com", + "login_uri": "https://login.cloud.huawei.com/oauth2/v3/token", "dev_app_id": "YOUR_REGISTRATION_APPID", + "dev_proj_id": "YOUR_REGISTRATION_PROJID", "client_secret": "YOUR_REGISTRATION_APPSECRET" } diff --git a/src/AGConnectAdmin/AGConnectAdmin.csproj b/src/AGConnectAdmin/AGConnectAdmin.csproj index 2915dc9..f21ef8f 100644 --- a/src/AGConnectAdmin/AGConnectAdmin.csproj +++ b/src/AGConnectAdmin/AGConnectAdmin.csproj @@ -1,25 +1,82 @@  - 1.0.0 - netstandard2.0;net45 + 1.0.2 + netstandard2.0;net45;net6.0 true ../AGConnectAdmin.snk true Huawei AGConnect Admin SDK enables server-side .NET developers to integrate HCM into their services and applications - Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + Copyright (c) Huawei Technologies Co., Ltd. 2019-2025. All rights reserved. Huawei Debug;Release;Proxy + README.md + https://github.com/HMS-Core/hms-push-serverdemo-csharp + https://github.com/HMS-Core/hms-push-serverdemo-csharp + LICENSE + ../build/$(MSBuildProjectName)/ + True + True + HMS Core Push Kit + $(Version) + $(Version) + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + 7 + + + + + True + \ + + + True + \ + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/AGConnectAdmin/ApiVersion.cs b/src/AGConnectAdmin/ApiVersion.cs new file mode 100644 index 0000000..3144c51 --- /dev/null +++ b/src/AGConnectAdmin/ApiVersion.cs @@ -0,0 +1,40 @@ +namespace AGConnectAdmin +{ + /// + /// Huawei PushKit API version level + /// + public enum ApiVersion + { + /// + /// API Base Uri V1 (App-level) + /// + V1, + /// + /// API Base Uri V2 (Project-level) + /// + V2 + } + + /// + /// ApiVersion Enum extension + /// + public static class ApiVersionExt + { + private const string _apiBaseUriPrefix = "https://push-api.cloud.huawei.com/v"; + private const string _appLevelApiBaseUri = _apiBaseUriPrefix + "1"; + private const string _projLevelApiBaseUri = _apiBaseUriPrefix + "2"; + + /// + /// Api version base Uri + /// + public static string ApiBaseUri(this ApiVersion apiVersion) + { + switch (apiVersion) + { + case ApiVersion.V1: return _appLevelApiBaseUri; + case ApiVersion.V2: return _projLevelApiBaseUri; + } + return string.Empty; + } + } +} diff --git a/src/AGConnectAdmin/AppOptions.cs b/src/AGConnectAdmin/AppOptions.cs index a3707c3..c661bc2 100644 --- a/src/AGConnectAdmin/AppOptions.cs +++ b/src/AGConnectAdmin/AppOptions.cs @@ -26,20 +26,32 @@ public sealed class AppOptions public AppOptions() { LoginUri = "https://oauth-login.cloud.huawei.com/oauth2/v3/token"; - ApiBaseUri = "https://push-api.cloud.huawei.com/v1"; + ApiVersion = ApiVersion.V2; } internal AppOptions(AppOptions options) { this.LoginUri = options.LoginUri; - this.ApiBaseUri = options.ApiBaseUri; + this.ApiVersion = options.ApiVersion; + this.ProjectId = options.ProjectId; this.ClientId = options.ClientId; this.ClientSecret = options.ClientSecret; } internal string GetApiUri() { - return ApiBaseUri + string.Format("/{0}/messages:send", ClientId) ; + var res = ApiBaseUri; + switch (ApiVersion) + { + case ApiVersion.V1: + res += '/' + ClientId; + break; + case ApiVersion.V2: + res += '/' + ProjectId; + break; + } + res += "/messages:send"; + return res; } /// @@ -48,10 +60,20 @@ internal string GetApiUri() public string LoginUri { get; set; } /// - /// Gets or sets the API base path, it's optional. + /// Gets or sets the PushKit API version. Default value is project-level (V2) /// This property is optional. /// - public string ApiBaseUri { get; set; } + public ApiVersion ApiVersion { get; set; } + + /// + /// Gets the API base uri according version + /// + public string ApiBaseUri { get => ApiVersion.ApiBaseUri(); } + + /// + /// Gets or sets the PROJECT ID from AGC + /// + public string ProjectId { get; set; } /// /// Gets or sets the APP ID from AGC. diff --git a/src/AGConnectAdmin/Directory.Build.props b/src/AGConnectAdmin/Directory.Build.props new file mode 100644 index 0000000..475b477 --- /dev/null +++ b/src/AGConnectAdmin/Directory.Build.props @@ -0,0 +1,5 @@ + + + ../build/$(MSBuildProjectName)/obj/ + + \ No newline at end of file