How does Amazon SNS Push Notifications to Mobile Devices – API

Overview

Push notifications are short, alert-style messages you can send to one or bulk of devices. Its send like simple text message. It uses Wi-Fi or cellular data.

Implementing push notifications is very easy, especially when you target multiple platforms such as iOS, Android. First you have to create account on Amazon SNS after then create Platform application for iOS and android. When any user login your application you need to generate endpointArn for that device. It will be a unique endpointArn.

 

pimgpsh_fullsize_distr3

 

 

What you need to do to create a mobile app that can receive push notifications?

 

Step 1: Request Credentials from Mobile Platforms

You must first request the necessary credentials from the mobile platforms.

 

fullsize_distr

 

Step 2: Request Token from Mobile Platforms

After create credential you will get return credentials to request a token for your mobile app and device from the mobile platforms. The token you receive represents your mobile app and device.

 

pimgpsh_fullsize_distr1

 

Step 3: Create Platform Application Object

The credentials and token are then used to create a platform application object (PlatformApplicationArn) from Amazon SNS.

 

fullsize_distr5

 

Step 4: Create Platform Endpoint Object

The PlatformApplicationArn is then used to create a platform named endpoint object (EndpointArn) from Amazon SNS.

 

pimgpsh_fullsize_distr4

 

Step 5: Publish Message to Mobile Endpoint

The EndpointArn is then used to publish a message to an app on a mobile device.

 

pimgpsh_fullsize_distr

 

You can send push notification messages to mobile devices using one of the following supported push notification services:

 

  • Apple Push Notification Service (APNS) for both iOS
  • Google Cloud Messaging for Android (GCM)

 

1 – Apple Push Notification Service (APNS) for both iOS

Apple Push Notification Service (APNS) is a service that enables you to send push notification messages to iOS apps.

APNS Prerequisites

To send push notifications to mobile devices using Amazon SNS and APNS, you need to obtain the following:

  • iOS app registered with APNS
  • APNS SSL certificate
  • App private key
  • Device token

 

To register your mobile app with AWS

  1. Go to https://console.aws.amazon.com/sns/v2/home and click Create platform application.
  2. In the Application name text-box, enter a name to represent your app.
  3. App name must be only uppercase and lowercase ASCII letters, numbers, underscores, hyphens, and periods, and must be between 1 and 256 characters long.
  4. In the Push notification platform text-box, select the platform that the app is registered with and then enter the appropriate credentials.

Note*

You must be select Choose file to upload the .p12 file (exported from Keychain Access) to APNS Platform.

2 – Google Cloud Messaging for Android (GCM)

Google Cloud Messaging for Android (GCM) is a service that enables you to send push notification messages to an Android app.

GCM Prerequisites

To send push notification messages to an Android app, you need the following step:

  • Android app registered with GCM
  • Registration ID
  • Server API key (sender auth token)

 

To create a Google API project and enable the GCM service

  1. On the Google APIs Console website, kindly verify that you have a Google API project.
  2. Click Services, & make sure Google Cloud Messaging for Android is worked up.

 

 

SIMPLE INTEGRATION OF AMAZON SNS API IN PHP

1) How to connect to Amazon SNS API With credential

//connect credentials to Amazon Sns
$sns = SnsClient::factory(array(
                      'version'   => 'latest',
                      'region'    => 'us-west-2',
                      'credentials' => array(
                        'key'       => '{KEY}', //eg. AKIAIGCDPR5RASASS
                        'secret'    => '{SECRET}', //eg. 9cVcIBe4zHLAIcC38+uWikoNB3prcNwwPz+ASASA
                      ),
           ));

 

2) How to Send Notification To IOS Device With endpointArn

//endpointArn device token for IOS
$endpointArn="arn:aws:sns:us-west-2:212274088127:endpoint/APNS/ProdIOS/956428ca-8052-31df-99";     

try{
     // this text message will get in IOS device
     $message=" Hello ,Push notification for iOS . ";                                                    
     $getResult = $sns->publish(array('Message' => $message, 'TargetArn' => $endpointArn));
     if($getResult['@metadata']['statusCode']=="200"){
             //sent
             echo " successfully sent notification to ios device. ";
      }
  }
   catch(Exception $e){
       //Exception error message
       echo $e->getMessage();  
  }

 

3) How to Send Notification To Android Device With endpointArn JSON Format

//endpointArn device token for android
$endpointArn="arn:aws:sns:us-west-2:212274088127:endpoint/GCM/ProdAnrd/6d68cee6-15fc-3690-92";

try{
      // this text message will get in IOS device
     $message=" Hello ,Push notification for Android . ";                                             
     $getResult = $sns->publish(array(
                        'Message' =>'{ "GCM": "{ \"data\": { \"m\": \"'.$message.'\" } }" }',
                        'TargetArn' =>$endpointArn,
                        'MessageStructure' => 'json'
                   ));

     if($getResult['@metadata']['statusCode']=="200")
                //sent
               echo " successfully sent notification to Android device. ";
       }
}
catch(Exception $e){
  //Exception error message
  echo $e->getMessage();     
}

 

Thanks!

My next blog will be coming soon!

 

 

Dnyaneshwar Jadhav

Dnyaneshwar - Well Experienced Professional in Web Development. Expertise in CakePHP | Laravel | Magento | Javascript | Ajax | JQuery | Holding an excellent experience of 4 years.

Dnyaneshwar Jadhav

About Dnyaneshwar Jadhav

Dnyaneshwar - Well Experienced Professional in Web Development. Expertise in CakePHP | Laravel | Magento | Javascript | Ajax | JQuery | Holding an excellent experience of 4 years.