How to get started with the AWS SDK for .NET in Yandex Cloud Notification Service
Note
The service is at the preview stage.
To get started with the AWS SDK for .NET:
- 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 .NET installation guide in the relevant AWS documentation
The guide below is for .NET 8.
-
Install
.NET Core SDK. -
Initialize your project in the selected directory:
dotnet new console --name example-app
-
Install the AWS SDK for .NET for SNS:
cd example-app/ dotnet add package AWSSDK.SimpleNotificationService
-
Create a client. Replace the
Program.cs
file contents with this code:using Amazon.Runtime; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; public static class ExampleSNS { static async Task Main(string[] args) { var endpoint = "https://notifications.yandexcloud.net/"; var credentials = new BasicAWSCredentials( "<static_key_ID>", "<secret_key>" ); var snsClient = new AmazonSimpleNotificationServiceClient( credentials, new AmazonSimpleNotificationServiceConfig { ServiceURL = endpoint, AuthenticationRegion = "ru-central1" } ); } }
Where
credentials
contains the static key ID and secret key. -
Build and run the project:
dotnet build dotnet run
Create a notification channel
try
{
var request = new CreatePlatformApplicationRequest
{
Name = "<channel_name>",
Platform = "<platform_type>",
Attributes = new Dictionary<string, string>
{
{ "<authentication_type>", "<key>" }
}
};
var response = await snsClient.CreatePlatformApplicationAsync(request);
Console.WriteLine("Platform application ARN: " + response.PlatformApplicationArn);
}
catch (Exception ex)
{
Console.WriteLine("Error creating platform application: " + ex.Message);
}
Where:
-
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).
-
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
-
Get a list of notification channels
var response = await snsClient.ListPlatformApplicationsAsync(new ListPlatformApplicationsRequest());
response.PlatformApplications.ForEach(app =>
Console.WriteLine($"Platform Application ARN: {app.PlatformApplicationArn}\n")
);
You will get the list of notification channels located in the same folder as the service account.
Create an endpoint
try
{
var response = await snsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest
{
PlatformApplicationArn = "<notification_channel_ARN>",
Token = "<push_token>"
});
Console.WriteLine($"Endpoint ARN: {response.EndpointArn}");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating platform endpoint: {ex.Message}");
}
Where:
PlatformApplicationArn
: Notification channel ID (ARN).Token
: Unique push token for the application on the user’s device.
Send a notification
Explicit notifications (Bright Push)
try
{
var targetArn = "<endpoint_ARN>";
var message = @"{
""default"": ""<notification_text>"",
""APNS"": ""{\""aps\"": {\""alert\"": \""<notification_text>\""}}""
}";
var messageStructure = "json";
var request = new PublishRequest
{
TargetArn = targetArn,
Message = message,
MessageStructure = messageStructure
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
catch (Exception ex)
{
Console.WriteLine("Error publishing message: " + ex.Message);
}
try
{
var targetArn = "<endpoint_ARN>";
var message = @"{
""default"": ""<notification_text>"",
""GCM"": ""{\""notification\"": {\""body\"": \""<notification_text>\""}}""}";
var messageStructure = "json";
var request = new PublishRequest
{
TargetArn = targetArn,
Message = message,
MessageStructure = messageStructure
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
catch (Exception ex)
{
Console.WriteLine("Error publishing message: " + ex.Message);
}
Where:
TargetArn
: Mobile endpoint ID (ARN)Message
: MessageMessageStructure
: Message format
Silent notifications (Silent Push)
try
{
var targetArn = "<endpoint_ARN>";
var message = @"{
""default"": ""<notification_text>"",
""APNS"": ""{\""key\"": \""value\""}""
}";
var messageStructure = "json";
var request = new PublishRequest
{
TargetArn = targetArn,
Message = message,
MessageStructure = messageStructure
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
catch (Exception ex)
{
Console.WriteLine("Error publishing message: " + ex.Message);
}
try
{
var targetArn = "<endpoint_ARN>";
var message = @"{
""default"": ""<notification_text>"",
""GCM"": ""{\""data\"": {\""key\"": \""value\""}}""}";
var messageStructure = "json";
var request = new PublishRequest
{
TargetArn = targetArn,
Message = message,
MessageStructure = messageStructure
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
catch (Exception ex)
{
Console.WriteLine("Error publishing message: " + ex.Message);
}
Where:
TargetArn
: Mobile endpoint ID (ARN)Message
: MessageMessageStructure
: Message format
Text message
try
{
var phoneNumber = "<phone_number>";
var message = "<notification_text>";
var request = new PublishRequest
{
PhoneNumber = phoneNumber,
Message = message,
MessageAttributes = new Dictionary<string, MessageAttributeValue>
{
{
"AWS.SNS.SMS.SenderID",
new MessageAttributeValue
{
DataType = "String",
StringValue = "<sender's_text_name>"
}
}
}
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
catch (Exception ex)
{
Console.WriteLine("Error sending SMS: " + ex.Message);
}
Where:
PhoneNumber
: Recipient's phone numberMessage
: Notification textStringValue
: Sender's text name