How to get started with the AWS SDK for Go in Yandex Cloud Notification Service
Note
The service is at the preview stage.
To get started with the AWS SDK for Go:
- Get your cloud ready.
- Get a static access key.
- Configure the AWS SDK.
- Create a notification channel.
- Get a list of channels.
- Create an endpoint.
- Send a notification.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Get a static access key
For authentication in Cloud Notification Service, use a static access key. The key is issued for the service account, and all actions are performed on behalf of that service account.
To get a static access key:
-
Create a service account.
-
Assign the
editor
role for the folder to the service account. -
Create a static access key for the service account.
Save the ID and secret key.
Configure the AWS SDK
You can find the prerequisites and an AWS SDK for Go installation guide in the relevant AWS documentation
-
Install
Go. -
Initialize the local project in the selected directory:
go mod init example
-
Install the required modules:
go get github.com/aws/aws-sdk-go-v2 go get github.com/aws/aws-sdk-go-v2/config go get github.com/aws/aws-sdk-go-v2/service/sns
-
Create a client:
package main import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/sns" ) var endpoint = string("https://notifications.yandexcloud.net/") var credProvider = aws.CredentialsProviderFunc(func(context.Context) (aws.Credentials, error) { cred := aws.Credentials{ AccessKeyID: "<static_key_ID>", SecretAccessKey: "<secret_key>", } return cred, nil }) func main() { config := aws.NewConfig() config.BaseEndpoint = &endpoint config.Region = "ru-central1" config.Credentials = credProvider client := sns.NewFromConfig(*config) }
Where:
AccessKeyID
: Static key IDSecretAccessKey
: Secret key
Create a notification channel
attributes := map[string]string{}
attributes["<authentication_type>"] = "<key>"
name := "<channel_name>"
platform := "<platform_type>"
createPlatformApplicationInput := sns.CreatePlatformApplicationInput{
Attributes: attributes,
Name: &name,
Platform: &platform,
}
createPlatformApplicationOutput, err := client.CreatePlatformApplication(context.TODO(), &createPlatformApplicationInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Platform application ARN:", *createPlatformApplicationOutput.PlatformApplicationArn)
}
Where:
-
attributes
: Mobile platform authentication parameters inkey=value
format. The values depend on platform:-
APNs:
-
Token-based authentication:
PlatformPrincipal
: Path to the token signature key file from ApplePlatformCredential
: Key IDApplePlatformTeamID
: Team IDApplePlatformBundleID
: Bundle ID
-
Certificate-based authentication:
-
PlatformPrincipal
: SSL certificate in.pem
format -
PlatformCredential
: Certificate private key in.pem
formatTo save the certificate and the private key in individual
.pem
files, use the openssl Linux utility:openssl pkcs12 -in Certificates.p12 -nokeys -nodes -out certificate.pem openssl pkcs12 -in Certificates.p12 -nocerts -nodes -out privatekey.pem
-
Token-based authentication: The more modern and secure method.
-
-
FCM:
PlatformCredential
is the Google Cloud service account key in JSON format for authentication with the HTTP v1 API or API key (server key) for authentication with the legacy API.Use the HTTP v1 API because the FCM legacy API is no longer supported
starting July 2024. -
HMS:
PlatformPrincipal
: Key IDPlatformCredential
: API key
-
-
name
: Notification channel name, user-defined. The name must be unique within the cloud. It may contain lowercase and uppercase Latin letters, numbers, underscores, hyphens, and periods. It may be from 1 to 256 characters long. For APNs channels, we recommend specifying the bundle ID in the name, and for FCM and HMS, the full package name. -
platform
: Mobile platform type:APNS
andAPNS_SANDBOX
: Apple Push Notification service (APNs). UseAPNS_SANDBOX
to test the application.GCM
: Firebase Cloud Messaging (FCM).HMS
: Huawei Mobile Services (HMS).
Get a list of notification channels
apps, err := client.ListPlatformApplications(context.TODO(), &sns.ListPlatformApplicationsInput{})
if err != nil {
log.Println(err.Error())
} else {
for _, app := range apps.PlatformApplications {
fmt.Println("Application ARN:", *app.PlatformApplicationArn)
}
}
You will get the list of notification channels located in the same folder as the service account.
Create an endpoint
appArn := "<notification_channel_ARN>"
token := "<push_token>"
createPlatformEndpointInput := sns.CreatePlatformEndpointInput{
PlatformApplicationArn: &appArn,
Token: &token,
}
createPlatformEndpointOutput, err := client.CreatePlatformEndpoint(context.TODO(), &createPlatformEndpointInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Endpoint ARN:", *createPlatformEndpointOutput.EndpointArn)
}
Where:
appArn
: Notification channel ID (ARN).token
: Unique push token for the application on the user’s device.
Send a notification
Explicit notifications (Bright Push)
targetArn := "<endpoint_ARN>"
message := `{"default": "<notification_text>", "APNS": "{\"aps\": {\"alert\": \"<notification_text>\"}}"}`
messageStructure := "json"
publishInput := sns.PublishInput{
TargetArn: &targetArn,
Message: &message,
MessageStructure: &messageStructure,
}
publishOutput, err := client.Publish(context.TODO(), &publishInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Message id:", *publishOutput.MessageId)
}
targetArn := "<endpoint_ARN>"
message := `{"default": "<notification_text>", "GCM": "{\"notification\": {\"body\": \"<notification_text>\"}}"}`
messageStructure := "json"
publishInput := sns.PublishInput{
TargetArn: &targetArn,
Message: &message,
MessageStructure: &messageStructure,
}
publishOutput, err := client.Publish(context.TODO(), &publishInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Message id:", *publishOutput.MessageId)
}
Where:
targetArn
: Mobile endpoint ID (ARN)messageStructure
: Message formatmessage
: Message
Silent notifications (Silent Push)
targetArn := "<endpoint_ARN>"
message := `{"default": "<notification_text>", "APNS": "{\"key\": \"value\"}"}`
messageStructure := "json"
publishInput := sns.PublishInput{
TargetArn: &targetArn,
Message: &message,
MessageStructure: &messageStructure,
}
publishOutput, err := client.Publish(context.TODO(), &publishInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Message id:", *publishOutput.MessageId)
}
targetArn := "<endpoint_ARN>"
message := `{"default": "<notification_text>", "GCM": "{\"data\": {\"key\": \"value\"}}"}`
messageStructure := "json"
publishInput := sns.PublishInput{
TargetArn: &targetArn,
Message: &message,
MessageStructure: &messageStructure,
}
publishOutput, err := client.Publish(context.TODO(), &publishInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Message id:", *publishOutput.MessageId)
}
Where:
targetArn
: Mobile endpoint ID (ARN)message
: MessagemessageStructure
: Message format
Text message
phoneNumber := "<phone_number>"
message := "<notification_text>"
dataType := "String"
stringValue := "<sender's_text_name>"
messageAttributes := map[string]types.MessageAttributeValue{}
messageAttributes["AWS.SNS.SMS.SenderID"] = types.MessageAttributeValue{
DataType: &dataType,
StringValue: &stringValue,
}
publishSMSInput := sns.PublishInput{
Message: &message,
PhoneNumber: &phoneNumber,
MessageAttributes: messageAttributes,
}
publishSMSOutput, err := client.Publish(context.TODO(), &publishSMSInput)
if err != nil {
log.Println(err.Error())
} else {
fmt.Println("Message id:", *publishSMSOutput.MessageId)
}
Where:
phoneNumber
: Recipient's phone numbermessage
: Notification textstringValue
: Sender's text name