Tips Send Mobile Notifications using Amazon SNS

phandaniel

New member
[TIẾNG VIỆT]:
** Cách gửi thông báo di động bằng Amazon SNS **

Dịch vụ thông báo đơn giản của Amazon (SNS) là một dịch vụ nhắn tin được quản lý đầy đủ của Pub/Sub giúp bạn dễ dàng tách rời các thành phần của các ứng dụng của bạn và gửi tin nhắn đáng tin cậy giữa chúng.SNS có thể được sử dụng để gửi thông báo đến thiết bị di động, địa chỉ email hoặc điểm cuối HTTP.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách gửi thông báo di động bằng Amazon SNS.Chúng tôi sẽ sử dụng SDK AWS Mobile cho Android để gửi thông báo đến các thiết bị Android.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Tài khoản AWS
* Môi trường phát triển Android
* SDK di động AWS cho Android

** Bước 1: Tạo chủ đề Amazon SNS **

Bước đầu tiên là tạo ra một chủ đề Amazon SNS.Một chủ đề là một nhóm hợp lý của các thông báo.Bạn có thể xuất bản tin nhắn lên một chủ đề và sau đó đăng ký chủ đề để nhận thông báo.

Để tạo một chủ đề, hãy truy cập bảng điều khiển Amazon SNS và nhấp vào ** Tạo chủ đề **.Nhập tên cho chủ đề của bạn và nhấp vào ** Tạo **.

** Bước 2: Đăng ký chủ đề **

Bây giờ bạn đã tạo ra một chủ đề, bạn cần đăng ký chủ đề.Điều này sẽ cho phép bạn nhận được thông báo khi tin nhắn được xuất bản đến chủ đề.

Để đăng ký chủ đề, hãy truy cập bảng điều khiển Amazon SNS và nhấp vào ** Đăng ký **.Chọn loại ** Loại điểm cuối ** làm ứng dụng ** ** và nhập thông tin sau:

*** điểm cuối: ** arn của ứng dụng Android của bạn
*** Giao thức: ** ** HTTP **
*** Chủ đề ARN: ** ARN của chủ đề bạn đã tạo ở bước trước

Nhấp vào ** Đăng ký **.

** Bước 3: Tạo thông báo di động **

Bây giờ bạn đã đăng ký chủ đề, bạn có thể tạo một thông báo di động.Thông báo di động là một thông báo được gửi đến thiết bị di động.Thông báo có thể bao gồm một tiêu đề, một cơ thể và một url.

Để tạo thông báo di động, bạn có thể sử dụng SDK AWS Mobile cho Android.AWS Mobile SDK cho Android cung cấp một số lớp mà bạn có thể sử dụng để gửi thông báo.

Trong hướng dẫn này, chúng tôi sẽ sử dụng lớp `AwsmobileClient` để gửi thông báo.Lớp `AwsmobileClient` cung cấp một phương thức gọi là` publish () `mà bạn có thể sử dụng để xuất bản một thông báo đến một chủ đề.

Mã sau đây cho thấy cách tạo thông báo di động và gửi nó đến một chủ đề:

`` `
AwsmobileClient Client = new AwsmobileClient (this);

Chuỗi chủ đề = "ARN: AWS: SNS: US-EAST-1: 123456789012: mytopic";

Thông báo thông báo = Thông báo mới ();
thông báo.settitle ("Tiêu đề thông báo của tôi");
thông báo.setbody ("Cơ quan thông báo của tôi");
thông báo.seturl ("Example Domain");

client.publish (chủ đề, thông báo);
`` `

** Bước 4: Nhận thông báo **

Khi bạn xuất bản một thông báo cho một chủ đề, người đăng ký vào chủ đề sẽ nhận được thông báo.Thông báo sẽ được gửi cho các thuê bao theo các cách sau:

*** http: ** Thông báo sẽ được gửi dưới dạng yêu cầu bài đăng HTTP đến URL mà bạn đã chỉ định khi bạn đăng ký chủ đề.
*** Email: ** Thông báo sẽ được gửi dưới dạng email đến địa chỉ email mà bạn đã chỉ định khi bạn đăng ký chủ đề.
*** SMS: ** Thông báo sẽ được gửi dưới dạng tin nhắn SMS đến số điện thoại mà bạn đã chỉ định khi bạn đăng ký chủ đề.

Trong hướng dẫn này, chúng tôi sẽ tập trung vào việc nhận thông báo về các thiết bị Android.

Để nhận thông báo trên các thiết bị Android, bạn cần thêm mã sau vào tệp `AndroidManifest.xml` của ứng dụng:

`` `
<sử dụng-permission android: name = "Android.Permission.Inet" />
<sử dụng-Permission Android: name = "Android.Permission.Receive_Boot_Completed" />
<sử dụng-permission android: name = "android.permission.wake_lock" />

<Ứng dụng ...>
<Người nhận
Android: name = "com.amazonaws.mobileconnector.iot.awsiotmqttreceiver"
Android: quyền = "Android.Permission.Receive_Boot_Completed">

[ENGLISH]:
**How to Send Mobile Notifications using Amazon SNS**

Amazon Simple Notification Service (SNS) is a fully managed pub/sub messaging service that makes it easy to decouple components of your applications and reliably send messages between them. SNS can be used to send notifications to mobile devices, email addresses, or HTTP endpoints.

In this tutorial, we will show you how to send mobile notifications using Amazon SNS. We will use the AWS Mobile SDK for Android to send notifications to Android devices.

**Prerequisites**

To follow this tutorial, you will need the following:

* An AWS account
* An Android development environment
* The AWS Mobile SDK for Android

**Step 1: Create an Amazon SNS topic**

The first step is to create an Amazon SNS topic. A topic is a logical grouping of notifications. You can publish messages to a topic and then subscribe to the topic to receive notifications.

To create a topic, go to the Amazon SNS console and click **Create topic**. Enter a name for your topic and click **Create**.

**Step 2: Subscribe to the topic**

Now that you have created a topic, you need to subscribe to the topic. This will allow you to receive notifications when messages are published to the topic.

To subscribe to the topic, go to the Amazon SNS console and click **Subscribe**. Select the **Endpoint type** as **Application** and enter the following information:

* **Endpoint:** The ARN of your Android app
* **Protocol:** **HTTP**
* **Topic ARN:** The ARN of the topic you created in the previous step

Click **Subscribe**.

**Step 3: Create a mobile notification**

Now that you have subscribed to the topic, you can create a mobile notification. A mobile notification is a message that is sent to a mobile device. The notification can include a title, a body, and a URL.

To create a mobile notification, you can use the AWS Mobile SDK for Android. The AWS Mobile SDK for Android provides a number of classes that you can use to send notifications.

In this tutorial, we will use the `AWSMobileClient` class to send a notification. The `AWSMobileClient` class provides a method called `publish()` that you can use to publish a message to a topic.

The following code shows how to create a mobile notification and send it to a topic:

```
AWSMobileClient client = new AWSMobileClient(this);

String topicArn = "arn:aws:sns:us-east-1:123456789012:MyTopic";

Notification notification = new Notification();
notification.setTitle("My notification title");
notification.setBody("My notification body");
notification.setUrl("Example Domain");

client.publish(topicArn, notification);
```

**Step 4: Receive the notification**

When you publish a notification to a topic, the subscribers to the topic will receive the notification. The notification will be delivered to the subscribers in the following ways:

* **HTTP:** The notification will be sent as an HTTP POST request to the URL that you specified when you subscribed to the topic.
* **Email:** The notification will be sent as an email to the email address that you specified when you subscribed to the topic.
* **SMS:** The notification will be sent as an SMS message to the phone number that you specified when you subscribed to the topic.

In this tutorial, we will focus on receiving notifications on Android devices.

To receive notifications on Android devices, you need to add the following code to your app's `AndroidManifest.xml` file:

```
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application ...>
<receiver
android:name="com.amazonaws.mobileconnectors.iot.AWSIotMqttReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top