Adventures of an Entrepreneur
  • Creating a Mobile Apps/Games Company

Setting up AWS SNS to send Push notifications to iOS devices

3/7/2014

32 Comments

 
Hi.

Today I will be showing you how to set up AWS SNS. The Amazon Web Services (AWS) Simple Notification Service (SNS) is a service that allows developers to use the AWS infrastructure to send Push notifications to devices (it can also be used to send SMS or messages to other services like the AWS Queue, but these are not in the scope of this tutorial).

Push notification are those messages that smartphone/tablet users receive that popups in the device screen. They are called as "Push" because it is the server that pushes the message to the device and not the app that requests the message from the server. 

Let's start.

#1) Log into your AWS Management Console and click on "SNS Services" under the "App Services" category.
Picture
#2) In the SNS dashboard, select the Region that you want to use for the SNS Service. Theoretically, you should select a region that is closer to your end users. We will be using N.Virginia for this tutorial.
Picture
#3) Click on "Add a New App"
Picture
#4) Fill the required info.
Application Name:  You can choose any app. I like to set a name that relates to the app that this SNS will provide the messages
Push Platform: You need to choose what platform will this SNS use. If your app is an iOS app, you need to choose the Apple Push Notification Service (APNS). During development, it is recommended to use the Apple Push Notification Service Sandbox (APNS_Sandbox). You also have the option to choose the Google service or Amazon. We will choose here Apple Push Notification Service Sandbox.
Credentials: in this section you will have to enter the credentials of the Push Platform selected above. On the APNS, we need to enter the Certificate and Private Key. We can enter them directly in the box or we can simply select the P12 private key file and the Certificate and Private Key info will be automatically filled. Let's do it by the simple way (choosing the file).
Picture
You probably are asking "Where is that p12 private file??". It is probably nowhere yet. We need to export it from the Keychain Access of your MAC developer machine. Of course, you already need to have it installed in your MAC, if you don't have it, please read my previous post on "Enabling Push Notifications for an iOS app". Let's export that private key then.

#4.1) Access your Keychain Access and select "Login" in the Keychains#4.2) Make a right click on the correct app private key and select Export.
Picture
#4.3) Set a password for the file.
Picture
#4.4) The system will ask for you MAC login password before exporting the file. 
Picture
$4.5) Save the p12 file somewhere. 
Picture
#5) Now, go back to the AWS SNS New App screen and select the private key that you just exported. It is a p12 file.
#6) Enter the password that you set on the item #4.3 and press "Load Credentials from File"
#7) Now, just click on "Add New App"
Picture
That is it. Now you have set the AWS SNS to send Push Notifications. 

You maybe thinking: "Ok, but how do I send a Push message?". Well first you need to register the device that will receive the Push Notification with the SNS service.

In SNS Dashboard of the app that you just configured, you can see the devices that are registered to receive the Push Notification. Since, we just configured the service, we have none. You need to make your app (in this case an iOS app) to register the user iPhone/iPad with the SNS Service. You do that by passing the device push token that Apple will provide to your app. If you already have that token and just want to test the SNS Service, you can add it manually by clicking on "Add Endpoints" (Endpoint in our environment here is a user device). The EndpoinArn info that you also see in the console is like the "AWS SNS token" for that device. After you add a iOS device token, the SNS you create a EndpointArn.

To send a Push notification to that device, just select it and go on "Endpoint Actions -> Publish". Just type the message and click on "Publish Message". If you set everything right, you should receive a Push notification on that device.

Of course, sending Push Messages manually for you game/app is not very fun. So, you need to have a server and configured it to communicate with the AWS SNS and send the messages for you.  AWS has SDKs for several languages (.NET, PHP,...), so you can get the SDK go thru their documentation to configure your server.
Picture
That is it. Hope that this tutorial helps you and if you want to know more about that or other mobile topic, just let me know.
32 Comments
Vinod link
3/31/2014 10:25:08 pm

Really very helpful

Reply
Rahul
6/18/2014 11:26:35 pm

how to apply apple notification via xcode for direct notification from one ios device to another.... pls explain with code(xcode)

Reply
Renato
6/19/2014 03:19:30 am

Hi Rahul.

For code examples, I suggest that you look for it on stackoverflow.com.

Reply
chandu
9/28/2014 07:35:34 pm

how can i do the same process in the programming using php.

thank you

Reply
Renato
9/29/2014 01:03:42 am

You can see how to do it via code (java) here: http://docs.aws.amazon.com/sns/latest/dg/using-awssdkjava.html

You can find the PHP SDK here: http://aws.amazon.com/sdk-for-php/

And also some PHP code here: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-sns.html

Reply
chandu
10/14/2014 03:03:09 pm

thank you soo much Renato..

Imran
10/12/2014 09:14:40 pm

Hey hi, really nice tutorial but i am facing a strange issue...the p12 file i created when i upload it says invalid credentials while the same p12 file get uploaded on parseSDK...can you suggest anything ?

Reply
Renato
10/13/2014 12:32:54 am

That error indicates that you are entering the wrong password (step #6). Make sure that you are entering the correct one or even try to export a new p12 with a different password.

Reply
Imran
10/13/2014 10:01:43 pm

Hey thanks for your quick reply....actually i am not setting any password on the p12 file....uploading it without password still it says invalid credentials....the same file get uploaded with parseSDK

Renato
10/14/2014 12:10:05 am

Did you try setting a password? Maybe Amazon requires the file to have a non-blank password....

Reply
Murali Viswanathan
10/14/2014 07:51:16 pm

Hello,
I tried with all the above steps,everything was working fine including device token, but when i try to publish from the aws console, the message does not bring notification to the device or is not getting delivered. what could be the issue.

Reply
Renato
10/15/2014 04:00:18 am

After making all the configuration above, you need to register your device with AWS. Assuming that you did that (and then AWS has your device token), you should be able to send a message using the panel. If the message is not arriving, maybe the app that you are running is not requesting Push Notification permission or you didn't give Push Notification permission when you opened the app.

Reply
dasarath sahoo
4/30/2015 10:57:47 pm

hi,

$this->sns = AWS::get('sns');
$task=$this->sns->publish(array(
'TargetArn' => 'arn:aws:sns:us-east-1:341707006720:Gameplandev/APNS_SANDBOX/Testtest/20a75cd1-da25-3331-8126-4db497cbdd5e',
'Message' => 'Hello from PHP',
'Subject' => 'testing'
));

this code not working, i am getting error with "Invalid parameter: Topic Name"

please support , how to pass device token on above code

Reply
Renato
5/1/2015 06:56:12 am

Hi there.

Unfortunately, I never used the AWS SDK for PHP. Please refer to the docs or post your question on stack overflow.

You can find the PHP SDK here: http://aws.amazon.com/sdk-for-php/

And also some PHP code here: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-sns.html

Reply
Gaurav
5/14/2015 01:27:22 pm

Hi,

I need your help . I am struggling from last 1 week .

Getting error while uploading .p12:-

Request could not be completed (Service: AmazonSNS; Status Code: 500; Error Code: InternalError; Request ID: 056f8177-091c-57d9-8e4f-ccb861e74d43)

Please help me.
Thanks in advance.

Reply
Renato
5/14/2015 01:30:08 pm

Do you see that when trying to upload de .p12 file via browser? Did you entered the p12 file password correctly?

Reply
hamy
6/2/2015 10:51:33 am

Hi, I have followed the steps to put .p12, password, certificate and private key. but when I tried to create application, it gaves me an error "APNS Sandbox certificate is invalid. Please verify the certificate can connect to APNS Sandbox by following this guide. If the certificate and private key are verified to work correctly and this issue is still occurring, please contact us. (Service: AmazonSNS; Status Code: 500; Error Code: InternalError)". I have tested my certificate and private key and they are valid.

Do you have any idea about this issue?

Thank you.

Reply
Renato
6/2/2015 11:11:14 am

Are u uploading the .p12 file or copying/pasting the contents? If copying/pasting, try to upload instead.

I would also suggest you to try setting up your push OneSignal (https://onesignal.com/) instead of AWS. If they give you the same problem, it is definitively something wrong with your certificates (try reissuing them).

R

Reply
Sean
7/6/2015 06:51:29 am

Uploading the file instead of pasting the .pem content worked for me. Thanks!

For those confused by the SNS interface: you have to choose the .p12, type in its associated password, and *then* click the "Generate from file" button

Amith
8/24/2015 07:12:48 pm

Tried with pem key and password, Personal Information Exchange File', it was not working;
Pasted the keys eg:'---Begin certificate', but gave this error- APNS Sandbox certificate is invalid. Please verify the certificate... ;
Is there any way out?

Ninza
8/10/2015 12:45:12 am

I have some issues for building aws server to push notification to all mobile devices using php script.
Who can help me?
I need your help.
Please help me.
Regards.
Paul

Reply
Rento
8/10/2015 05:41:01 am

Sorry, I didn't use the PHP SDK. Please look for your problem solution at stackoverflow.com

Reply
sahil
9/2/2015 11:56:53 pm

great tutorial. very detailed description with images.

Reply
Prateek
9/8/2015 05:52:52 am

I have to implement the AWS SNS in PHP. I have a live app on App store and will get it installed on my iphone. How can i get the my device push token that i have to use to get the ARN from AWS SNS?

Reply
Renato
9/8/2015 06:40:27 am

Hi Prateek9.

That depends on how you are coding your app.

If you are coding natively, you can take advantage of the AWS SDK for iOS in order to get device token.

If you don't want to use the AWS SDK or are not coding natively, you can use AWS Rest API in order to pass the token (Just a note, it is recommended for you to use your own server as a middle man between the app and AWS Servers, so you don't have to expose your AWS Credentials inside the app).

Reply
MarkX
9/24/2015 10:06:20 am

Thanks a lot! it is really helpful.

Reply
aws training link
9/21/2016 11:06:06 pm

Wow. That is so elegant and logical and clearly explained. Brilliantly goes through what could be a complex process and makes it obvious.

Reply
Anji
6/12/2018 06:10:01 am

I have implemented APNS messaging in my app for push notifications in AWS. Everything working fine. But notifications are getting even user logged out from app

Reply
Renato
6/12/2018 01:50:03 pm

You need to manually de-register the device when the user logout.

Reply
Bhanupratap link
3/27/2019 04:24:18 am

Just found your post by searching on the Google, I am Impressed and Learned Lot of new thing from your post. I am new to blogging and always try to learn new skill as I believe that blogging is the full time job for learning new things day by day.

Reply
hemanth link
4/4/2019 11:33:25 pm

i just go through your article it’s very interesting time just pass away by reading your article looking for more updates. Thank you for sharing.

Reply
Stacy M link
11/25/2020 09:08:05 pm

Great reading your post

Reply



Leave a Reply.

    Author

    This blog it not updated anymore.  

    Archives

    February 2015
    June 2014
    May 2014
    April 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    September 2012
    August 2012
    July 2012

    Categories

    All
    Data
    Event
    Fun
    Games
    Key Success Factors
    Management
    Marketing
    Mobile
    Monetization
    Movies
    Startup
    Statistics
    Technical
    Tutorial
    Videos

Powered by Create your own unique website with customizable templates.