Skip to content

HMS-Core/hms-push-serverdemo-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HMS Core Push Kit Sample Code (Golang)

English | 中文

Contents

Introduction

The sample code for Golang encapsulates the server-side APIs of Push Kit, for your reference or direct use.

The following table describes packages of Golang sample code.

Package Description
examples Sample code packages.
httpclient Package for sending network requests.
push Package where Push Kit server APIs are encapsulated.

Installation

Install the Golang environment (Golang 1.11 or later is recommended) and decompress the Golang sample code package.

Copy org.huawei.com in the decompressed package to the vendor directory in the path specified by GOPATH in your project. Refresh the project to ensure that the copied file exists in the directory.

Configuration

Start configuration with the Client structure in the push package. Each method in the Client structure can be used to call an API of the Push Kit server. The following table describes the method in the Client structure.

Method Description
SendMessage Sends a message to a device.

To use the functions provided by the packages in examples, set related parameters in pushcommon.go in the common package.

The following table describes the parameters in pushcommon.go.

Parameter Description
appId App ID, which is obtained from the app information.
appSecret App secret, which is obtained from the app information.
authUrl URL for Huawei OAuth 2.0 to obtain a token. For details, please refer to OAuth 2.0-based Authentication.
pushUrl Access address of Push Kit. For details, please refer to Downlink Message Sending.

The following table describes the parameters in pushcommon.go.

Parameter Description
TargetTopic Name of the topic to be subscribed to, unsubscribed from, or queried.
TargetCondition Combined condition expression for sending a message.
TargetToken Token of a target device, which is obtained from the target device.
TargetTokenArray Tokens of all target devices, which are obtained from these target devices.

Sample Code

1). Send an Android data message. You can obtain the initialized MessageRequest instance of the data message using the NewTransparentMsgRequest method in the push/model package.

Code location: send_data_message

2). Send an Android notification message. You can obtain the initialized MessageRequest instance of the notification message using the NewNotificationMsgRequest method in the push/model package.

Code location: send_notify_message

3). Send a message by topic. You can send a notification message or data message to a device by topic. Specify the topic after obtaining the MessageRequest instance.

Code location: send_topic_message

4). Send a message by conditions. You can send a notification message or data message to a device by conditions. Specify the conditions after obtaining the MessageRequest instance.

Code location: send_condition_message

5). Send a message to a Huawei quick app. You can send a message to a quick app by setting FastAppTarget.

Code location: send_instance_app_message

6). Send a message through the APNs agent. You can send a message through the APNs agent by setting Apns of the message.

Code location: send_apns_message

7). Send a message through the WebPush agent. You can send a message through the WebPush agent by setting WebPush of the message.

Code location: send_webpush_message

8). Send a test message.

Code location: send_test_message

Technical Support

You can visit the Reddit community to obtain the latest information about HMS Core and communicate with other developers.

If you have any questions about the sample code, try the following:

  • Visit Stack Overflow, submit your questions, and tag them with huawei-mobile-services. Huawei experts will answer your questions.
  • Visit the HMS Core section in the HUAWEI Developer Forum and communicate with other developers.

If you encounter any issues when using the sample code, submit your issues or submit a pull request.

License

The sample code is licensed under Apache License 2.0.