Mengirim Pemberitahuan Dari Cloud Menggunakan Amazon Simple Notification Service - AWS SDK untuk .NET (V4)

Versi 4 (V4) dari AWS SDK untuk .NET telah dirilis!

Untuk informasi tentang melanggar perubahan dan memigrasi aplikasi Anda, lihat topik migrasi.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Mengirim Pemberitahuan Dari Cloud Menggunakan Amazon Simple Notification Service

catatan

Informasi dalam topik ini khusus untuk proyek berdasarkan .NET Framework dan AWS SDK untuk .NET versi 3.3 dan sebelumnya.

Ini AWS SDK untuk .NET mendukung Amazon Simple Notification Service (Amazon SNS), yang merupakan layanan web yang memungkinkan aplikasi, pengguna akhir, dan perangkat untuk langsung mengirim notifikasi dari cloud. Untuk informasi selengkapnya, lihat Amazon SNS.

Daftar Topik Amazon SNS Anda

Contoh berikut menunjukkan cara mencantumkan topik Amazon SNS Anda, langganan untuk setiap topik, dan atribut untuk setiap topik. Contoh ini menggunakan default AmazonSimpleNotificationServiceClient.

// using Amazon.SimpleNotificationService; // using Amazon.SimpleNotificationService.Model; var client = new AmazonSimpleNotificationServiceClient(); var request = new ListTopicsRequest(); var response = new ListTopicsResponse(); do { response = client.ListTopics(request); foreach (var topic in response.Topics) { Console.WriteLine("Topic: {0}", topic.TopicArn); var subs = client.ListSubscriptionsByTopic( new ListSubscriptionsByTopicRequest { TopicArn = topic.TopicArn }); var ss = subs.Subscriptions; if (ss.Any()) { Console.WriteLine(" Subscriptions:"); foreach (var sub in ss) { Console.WriteLine(" {0}", sub.SubscriptionArn); } } var attrs = client.GetTopicAttributes( new GetTopicAttributesRequest { TopicArn = topic.TopicArn }).Attributes; if (attrs.Any()) { Console.WriteLine(" Attributes:"); foreach (var attr in attrs) { Console.WriteLine(" {0} = {1}", attr.Key, attr.Value); } } Console.WriteLine(); } request.NextToken = response.NextToken; } while (!string.IsNullOrEmpty(response.NextToken));

Mengirim Pesan ke Topik Amazon SNS

Contoh berikut menunjukkan cara mengirim pesan ke topik Amazon SNS. Contohnya mengambil satu argumen, ARN dari topik Amazon SNS.

using System; using System.Linq; using System.Threading.Tasks; using Amazon; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SnsSendMessage { class Program { static void Main(string[] args) { /* Topic ARNs must be in the correct format: * arn:aws:sns:REGION:ACCOUNT_ID:NAME * * where: * REGION is the region in which the topic is created, such as us-west-2 * ACCOUNT_ID is your (typically) 12-character account ID * NAME is the name of the topic */ string topicArn = args[0]; string message = "Hello at " + DateTime.Now.ToShortTimeString(); var client = new AmazonSimpleNotificationServiceClient(region: Amazon.RegionEndpoint.USWest2); var request = new PublishRequest { Message = message, TopicArn = topicArn }; try { var response = client.Publish(request); Console.WriteLine("Message sent to topic:"); Console.WriteLine(message); } catch (Exception ex) { Console.WriteLine("Caught exception publishing request:"); Console.WriteLine(ex.Message); } } } }

Lihat contoh lengkapnya, termasuk informasi tentang cara membuat dan menjalankan contoh dari baris perintah, pada GitHub.

Mengirim Pesan SMS ke Nomor Telepon

Contoh berikut menunjukkan cara mengirim pesan SMS ke nomor telepon. Contohnya mengambil satu argumen, nomor telepon, yang harus dalam salah satu dari dua format yang dijelaskan dalam komentar.

using System; using System.Linq; using System.Threading.Tasks; using Amazon; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SnsPublish { class Program { static void Main(string[] args) { // US phone numbers must be in the correct format: // +1 (nnn) nnn-nnnn OR +1nnnnnnnnnn string number = args[0]; string message = "Hello at " + DateTime.Now.ToShortTimeString(); var client = new AmazonSimpleNotificationServiceClient(region: Amazon.RegionEndpoint.USWest2); var request = new PublishRequest { Message = message, PhoneNumber = number }; try { var response = client.Publish(request); Console.WriteLine("Message sent to " + number + ":"); Console.WriteLine(message); } catch (Exception ex) { Console.WriteLine("Caught exception publishing request:"); Console.WriteLine(ex.Message); } } } }

Lihat contoh lengkapnya, termasuk informasi tentang cara membuat dan menjalankan contoh dari baris perintah, pada GitHub.