Home

Awesome

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.

PackageDescription
examplesSample code packages.
httpclientPackage for sending network requests.
pushPackage 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.

MethodDescription
SendMessageSends 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.

ParameterDescription
appIdApp ID, which is obtained from the app information.
appSecretApp secret, which is obtained from the app information.
authUrlURL for Huawei OAuth 2.0 to obtain a token. For details, please refer to OAuth 2.0-based Authentication.
pushUrlAccess address of Push Kit. For details, please refer to Downlink Message Sending.

The following table describes the parameters in pushcommon.go.

ParameterDescription
TargetTopicName of the topic to be subscribed to, unsubscribed from, or queried.
TargetConditionCombined condition expression for sending a message.
TargetTokenToken of a target device, which is obtained from the target device.
TargetTokenArrayTokens 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:

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.