From 2e143d577254a18b3f095021fd71f91a3e82b144 Mon Sep 17 00:00:00 2001 From: elquimeras Date: Fri, 7 May 2021 13:13:27 -0400 Subject: [PATCH] Update README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index de00dad..2d1a167 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # hcm-node + Node.js package code encapsulates APIs of the HUAWEI Push Kit server. ## Installation @@ -6,3 +7,43 @@ Node.js package code encapsulates APIs of the HUAWEI Push Kit server. ```bash $ npm i hcm-node ``` + +## Usage + +```typescript +import HCM from 'hcm-node'; +import { Messaging } from 'hcm-node/dist/push/messaging'; + +const hcm = new HCM(); +hcm.init({ + appId: '', + appSecret: '', + authUrl: 'https://oauth-login.cloud.huawei.com/oauth2/v2/token', + pushUrl: 'https://push-api.cloud.huawei.com/v1' +); +const hcmDispatcher = hcm.messaging().messaging; + +try { + const response = hcmDispatcher.send(message, false); + + /** + * HCM Response code for success = 80000000 + * more info @ https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-sendapi + */ + if (response.code != '80000000') { + throwError(response); + } +} catch(error) { + throwError(error); +} + +function throwError(detail: any): void { + console.error({ + message: 'push notification dispatch failed', + details: error, + }); + + throw new Error('External error'); +} + +```