Menu
Amazon Simple Notification Service
Developer Guide (API Version 2010-03-31)

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

  1. In Eclipse, create a new Java project (File | New | Java Project).

  2. 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 the SNSSamples folder, click OK, and then click Finish.

  3. 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

  1. Right-click the Java project folder, click Build Path, and then click Configure Build Path...

  2. Click the Libraries tab, and then click Add Library....

  3. Click AWS SDK for Java, click Next, and then click Finish.

To add the prerequisite information to SNSMobilePush.java

  1. In the SNSSamples\src\com\amazonaws\sns\samples\mobilepush folder, open SNSMobilePush.java in Eclipse and uncomment sample.demoBaiduAppNotification();. It should look similar to the following:

    Copy
    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();
  2. 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:

    Copy
    String userId = "EXAMPLE-kLMchcX0v3xOxWVhG6TfdBp...KT2TGkvnKyTvLuSpzK_qsHgxVB_UpmcUa7Gl6g3EXAMPLE"; String channelId = "EXAMPLE<channelId>EXAMPLE";
  3. Enter the secret key for your application. For example, it should look similar to the following:

    Copy
    String secretKey = "EXAMPLE<secretkey>EXAMPLE";
  4. Enter the API key for your application. For example, it should look similar to the following:

    Copy
    String apiKey = "EXAMPLExV2lcV2zEKTLNYs625zfk2jh4EXAMPLE";
  5. 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:

    Copy
    String applicationName = "baidupushapp";
  6. Run the application. You should see output similar to the following in the output window of your IDE:

    Copy
    =========================================== 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: