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

Send a push notification message to an iOS app using Amazon SNS and APNS

This section describes how to use the prerequisite information with the Amazon SNS API to send a push notification message to your iOS app using Amazon SNS and APNS. You add the prerequisite information to the AWS sample file SNSMobilePush.java, which is included in the snsmobilepush.zip file.

You can also use the Amazon SNS console. For more information about using the Amazon SNS console, see Using Amazon SNS Mobile Push.

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. Create a new Java project in Eclipse (File | New | Java Project).

  2. Import the SNSSamples folder to the top-level directory of the newly created Java project. In Eclipse, right-choose the name of the Java project and then choose Import, expand General, choose File System, choose Next, browse to the SNSSamples folder, choose OK, and then choose 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-choose the Java project folder, choose Build Path, and then choose Configure Build Path...

  2. Choose the Libraries tab, and then choose Add Library....

  3. Choose AWS SDK for Java, choose Next, and then choose 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.

  2. Depending on which APNS you are using, uncomment either sample.demoAppleAppNotification(); or sample.demoAppleSandboxAppNotification();. For example, if you're using demoAppleSandboxAppNotification, it should look similar to the following:

    SNSMobilePush sample = new SNSMobilePush(sns); // Uncomment the services you wish to use. // sample.demoAndroidAppNotification(); // sample.demoKindleAppNotification(); // sample.demoAppleAppNotification(); sample.demoAppleSandboxAppNotification(); // sample.demoBaiduAppNotification(); // sample.demoWNSAppNotification(); // sample.demoMPNSAppNotification();
  3. Locate the demoAppleSandboxAppNotification method and enter the device token you received from APNS for the value of the device token string. For example, it should look similar to the following:

    String deviceToken = "examp1e29z6j5c4df46f809505189c4c83fjcgf7f6257e98542d2jt3395kj73";
  4. Enter the APNS SSL certificate for your app. At the beginning of each new line in your certificate, you must add \n. For example, it should look similar to the following:

    String certificate = "-----BEGIN CERTIFICATE-----\nMIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9w0BAQUFADCBiDELMAkGA1UEBhMC\nVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6\nb24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAd\nBgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wHhcNMTEwNDI1MjA0NTIxWhcN\nMTIwNDI0MjA0NTIxWjCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYD\nVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25z\nb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFt\nYXpvbi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMaK0dn+a4GmWIWJ\n21uUSfwfEvySWtC2XADZ4nB+BLYgVIk60CpiwsZ3G93vUEIO3IyNoH/f0wYK8m9T\nrDHudUZg3qX4waLG5M43q7Wgc/MbQITxOUSQv7c7ugFFDzQGBzZswY6786m86gpE\nIbb3OhjZnzcvQAaRHhdlQWIMm2nrAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAtCu4\nnUhVVxYUntneD9+h8Mg9q6q+auNKyExzyLwaxlAoo7TJHidbtS4J5iNmZgXL0Fkb\nFFBjvSfpJIlJ00zbhNYS5f6GuoEDmFJl0ZxBHjJnyp378OD8uTs7fLvjx79LjSTb\nNYiytVbZPQUQ5Yaxu2jXnimvw3rrszlaEXAMPLE=\n-----END CERTIFICATE-----";
  5. Enter the private key for your app. At the beginning of each new line in your certificate, you must add \n. For example, it should look similar to the following:

    String privateKey = "-----BEGIN RSA PR1VATE KEY-----\nMJICiTCHAfICCQD9m7oRw0uXOjANBgkqhkiG7w0BAQUFADCBiDELMAkGA1UEBhMC\nWVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGx2MQ8wDQYDVQQKEwZBbWF6\nVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6\n4MXNchZOFFreg4Rr3Xzhb9RhvlIRgsr3wU4/FYai3z96EXAMPLE=\n-----END RSA PR1VATE KEY-----";
  6. Enter a name for your app. App 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 = "mypushappname";
  7. Run the Java 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/APNS_SANDBOX/mypushappname} {EndpointArn: arn:aws:sns:us-west-2:111122223333:endpoint/APNS_SANDBOX/pushapp/97e9ced9-f136-3893-9d60-775467eafebb} {"default":"This is the default Message","APNS_SANDBOX":"{ \"aps\" : { \"alert\" : \"You have got email.\", \"badge\" : 9,\"sound\" :\"default\"}}"} Published. MessageId=d65fb4bb-b903-5e37-83d4-feb4818d6da3

    On your iOS device, you should see a message notification.