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
    • 52
    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

    <?php 
    
        public function notifyiPhone($token,$msg)
        {
          // For development we use 'gateway.sandbox.push.apple.com' 
          $apnsHost ='gateway.push.apple.com'; // This is for production
          $apnsCert = '/etc/file'; // this is a file with .pem extention    
          $apnsPort = 2195;  
          $passphrase =1234; 
          $error = true;
          $errorString = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
       
          $ctx = stream_context_create();
    
          stream_context_set_option($ctx, 'ssl', 'local_cert',$apnsCert);
    	  
          $stream=stream_context_set_option($ctx, 'ssl', 'passphrase',$passphrase);
          $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
    		 
          if($apns === False){
    		return;
          }            
          $payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
          $output = json_encode($payload);
          $token = pack('H*', str_replace(' ','',$token));
          $apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
          $result =fwrite($apns, $apnsMessage);
        
    	  
    	  if (!$result)
            return  'Message not delivered ';
          else
            return $msg ;
          
    	  @socket_close($apns);
          fclose($apns);
    	  
        }
    
    ?>

    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

    <?php 
    
        public function notifyAndroid($reg_id, $message)
        {
          $fields = array(
            'registration_ids'  => array( $reg_id ),
            'data'              => array( "message" => $message ),
            );
          $headers = array(
            'Authorization: key = AzklmyAGOyh1234546jigVlwnps_rf9gZsdtI', // This is the API key
            'Content-Type: application/json'
          );
    
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
          $result = curl_exec($ch);
          if ($result === FALSE) {
            die('Problem occurred: ' . curl_error($ch));
          }
          curl_close($ch);
        }
    
    ?>

    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: