0.0.3 • Published 3 years ago

hcm-node v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

hcm-node

Node.js package code encapsulates APIs of the HUAWEI Push Kit server.

Installation

$ npm i hcm-node

Usage

import HCM from 'hcm-node';
import { Messaging } from 'hcm-node/dist/push/messaging';

const hcm = new HCM();
hcm.init({
  appId: '<YOUR-APP-ID>',
  appSecret: '<YOUR-APP-SECRET>',
  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');
}

Configuration

Initialization ParameterDescription
AppIdApp ID, which is obtained from app information.
AppSecretSecret access key of an app, which is obtained from app information.
AuthUrlURL for the Huawei OAuth 2.0 service to obtain a token, please refer to Generating an App-Level Access Token.
PushUrlURL for accessing HUAWEI Push Kit, please refer to Sending Messages.
Request ParameterDescription
TargetTopicName of a topic to be subscribed to, unsubscribed from, or queried.
TargetConditionCombination of condition expressions for a message.
TargetTokenToken of a destination device.
TargetTokenArrayTokens of destination devices.