Step 6: Send a Push Notification Message to a Mobile Endpoint using Amazon SNS and Baidu
This section describes how to send a push notification message to your mobile
endpoint. You add the gathered prerequisite information to the AWS sample file
SNSMobilePush.java
, which is included in the snsmobilepush.zip file. Included in the SNSMobilePush.java
file are examples on how to create a mobile endpoint
and use message attributes for structuring the message. For additional information
and examples on how to create mobile endpoints and use message attributes with Baidu,
see Creating an Amazon SNS Endpoint for Baidu and Using Message Attributes for Structuring the
Message.
Note
The following steps use the Eclipse Java IDE. The steps assume you have installed the AWS SDK for Java and you have the AWS security credentials for your AWS account. For more information, see AWS SDK for Java. For more information about credentials, see How Do I Get Security Credentials? in the AWS General Reference.
To add the sample to Eclipse
-
In Eclipse, create a new Java project (File | New | Java Project).
-
Import the
SNSSamples
folder to the top-level directory of the newly created Java project. In Eclipse, right-click the name of the Java project and then click Import, expand General, click File System, click Next, browse to theSNSSamples
folder, click OK, and then click Finish. -
In the SNSSamples\src\com\amazonaws\sns\samples\mobilepush folder, open the
AwsCredentials.properties
file and add your AWS security credentials.
To add the AWS SDK for Java to the Build Path
-
Right-click the Java project folder, click Build Path, and then click Configure Build Path...
-
Click the Libraries tab, and then click Add Library....
-
Click AWS SDK for Java, click Next, and then click Finish.
To add the prerequisite information to SNSMobilePush.java
-
In the SNSSamples\src\com\amazonaws\sns\samples\mobilepush folder, open
SNSMobilePush.java
in Eclipse and uncommentsample.demoBaiduAppNotification();
. It should look similar to the following:SNSMobilePush sample = new SNSMobilePush(sns); // TODO: Uncomment the services you wish to use. //sample.demoAndroidAppNotification(); //sample.demoKindleAppNotification(); //sample.demoAppleAppNotification(); //sample.demoAppleSandboxAppNotification(); sample.demoBaiduAppNotification(); //sample.demoWNSAppNotification(); //sample.demoMPNSAppNotification();
-
Locate the
demoBaiduAppNotification
method and enter the user ID and channel ID you received from Baidu for the value of the userId and channelId strings. For example, it should look similar to the following:String userId = "EXAMPLE-kLMchcX0v3xOxWVhG6TfdBp...KT2TGkvnKyTvLuSpzK_qsHgxVB_UpmcUa7Gl6g3EXAMPLE"; String channelId = "EXAMPLE<channelId>EXAMPLE";
-
Enter the secret key for your application. For example, it should look similar to the following:
String secretKey = "EXAMPLE<secretkey>EXAMPLE";
-
Enter the API key for your application. For example, it should look similar to the following:
String apiKey = "EXAMPLExV2lcV2zEKTLNYs625zfk2jh4EXAMPLE";
-
Enter a name for your application. Application names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, hyphens, and periods, and must be between 1 and 256 characters long. For example, it should look similar to the following:
String applicationName = "baidupushapp";
-
Run the application. You should see output similar to the following in the output window of your IDE:
=========================================== Getting Started with Amazon SNS =========================================== {PlatformApplicationArn: arn:aws:sns:us-west-2:111122223333:app/BAIDU/TestApp} {EndpointArn: arn:aws:sns:us-west-2:111122223333:endpoint/BAIDU/TestApp/8f3fdf0d-520b-38d1-8ed2-3301a477eef3} {Message Body: {"BAIDU":"{\"title\":\"New Notification Received from SNS\",\"description\":\"Hello World!\"}"}} {Message Attributes: ("AWS.SNS.MOBILE.BAIDU.MessageKey": "default-channel-msg-key"),("AWS.SNS.MOBILE.BAIDU.DeployStatus": "1"),("AWS.SNS.MOBILE.BAIDU.MessageType": "0")} Published! {MessageId=56a3a3e6-4b4b-59b4-8d1d-eff592c0ffa1}
On your Android device, you should see a push notification message appear within the Android app, similar to the following: