Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send pushnotifications via iPhone/Android devices

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 116
    Comment on it

    In this tutorial we will learn how to send the pushnotifications iPhone/ Android apps.

    Below is the code that requires the follwoing elements to send the pushnotification from iPhone

    1-) The device token to which we need to send the pushnotification

    2-) The message

    3- ) We can also send other information in the payload

    1. <?php
    2.  
    3. public function notifyiPhone($token,$msg)
    4. {
    5. // For development we use 'gateway.sandbox.push.apple.com'
    6. $apnsHost ='gateway.push.apple.com'; // This is for production
    7. $apnsCert = '/etc/file'; // this is a file with .pem extention
    8. $apnsPort = 2195;
    9. $passphrase =1234;
    10. $error = true;
    11. $errorString = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
    12. $ctx = stream_context_create();
    13.  
    14. stream_context_set_option($ctx, 'ssl', 'local_cert',$apnsCert);
    15.     
    16. $stream=stream_context_set_option($ctx, 'ssl', 'passphrase',$passphrase);
    17. $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
    18.         
    19. if($apns === False){
    20.         return;
    21. }
    22. $payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
    23. $output = json_encode($payload);
    24. $token = pack('H*', str_replace(' ','',$token));
    25. $apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
    26. $result =fwrite($apns, $apnsMessage);
    27.     
    28.      if (!$result)
    29. return 'Message not delivered ';
    30. else
    31. return $msg ;
    32.      @socket_close($apns);
    33. fclose($apns);
    34.     
    35. }
    36.  
    37. ?>

    Below is the code that requires the follwoing elements to send the pushnotification from Android

     

    1-) The device token to which we need to send the pushnotification

    2-) The message

    3- ) We can also send other information in the payload

    1. <?php
    2.  
    3. public function notifyAndroid($reg_id, $message)
    4. {
    5. $fields = array(
    6. 'registration_ids' => array( $reg_id ),
    7. 'data' => array( "message" => $message ),
    8. );
    9. $headers = array(
    10. 'Authorization: key = AzklmyAGOyh1234546jigVlwnps_rf9gZsdtI', // This is the API key
    11. 'Content-Type: application/json'
    12. );
    13.  
    14. $ch = curl_init();
    15. curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
    16. curl_setopt($ch, CURLOPT_POST, true);
    17. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    19. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    20. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    21. $result = curl_exec($ch);
    22. if ($result === FALSE) {
    23. die('Problem occurred: ' . curl_error($ch));
    24. }
    25. curl_close($ch);
    26. }
    27.  
    28. ?>

    For more information see the below links:

    https://gist.github.com/joashp/b2f6c7e24127f2798eb2

    http://docs.pushwoosh.com/docs/gcm-configuration

    http://docs.pushwoosh.com/docs/apns-configuration

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: