Push Notifications for Android and iOS App in PHP

Today’s trend to Android and iOS applications for the push notifications, So push notification is the basic and most important feature of every new generation application. Today generation people up to date by social notification like WhatsApp and facebook notification.

If you want to integrate push notification to own Android and iOS application. You will need to integrate push notification code, we have provided you working and tested Android and iOS push notification in PHP web services.

<?php

function send_push_notification($token_id,$tAlert,$badge=NULL){ 
    
     if(strlen($token_id)>100){
            $registrationIds = array($token_id);
            $msg = array
            (
            'message' => $tAlert,
            'title' => "wewebtech",
            'vibrate' => 1,
            'sound' => 'default'

            // you can also add images, additionalData
            );

            $fields = array
            (
            'registration_ids' => $registrationIds,
            'data' => $msg
            );
            /*You can definr API_ACCESS_KEY in thr config file*/
            $headers = array
            (
            'Authorization: key=' . API_ACCESS_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 );
            curl_close( $ch );
            return $result;
          }
          else
          {             

                $tHost = 'gateway.sandbox.push.apple.com';
                $tPort = 2195;
                $tCert = './assets/wewebtech.pem';
                $tPassphrase = '1234';               
                $sounds = 'sendcheers.mp3';
                                
                $ctx = stream_context_create();
                stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
                stream_context_set_option($ctx, 'ssl', 'passphrase', $tPassphrase);
                $fp = stream_socket_client('ssl://'.$tHost.':'.$tPort, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
                if (!$fp)
                 return "APNS Connection Failed: $err $errstr" . PHP_EOL;
                $results = array();
                $tPayload = $tAlert;
                $tnot='0';
                $tBadge ='0'; 
                $tBody_new['aps'] = array (
                //'alert' => $message,
                'alert' => $tAlert['message'],
                'badge' => $tBadge,
                'sound'  => $sounds,
                //'sound'  =>'letgo_notification.mp3',
                'tnot'=>$tnot
                ); 
                $tBody_new ['payload'] = $tPayload;
                $tBody = json_encode ($tBody_new);
                $msg = chr(0) . pack('n', 32) . pack('H*', $token_id) . pack('n', strlen($tBody)) . $tBody;
                //Send it to the server
                $results = fwrite($fp, $msg, strlen($msg));
                //Close the connection to the server
                //fclose($fp);
                if (empty($results))
                return FALSE;
                else
                return $results;
                fclose($fp);

          }
}

?>

This function you can use to in the helper function.

Now you can use this function in the controller according to conditions:

<?php            
           $pushdata  = array(                              
                'push_type' => '1',                            
                'message' =>    'User registred successfully',   
            );                
            send_push_notification($_POST['deviceId'],$pushdata);
?>

If there any issues regarding this article please comment on the same.

Thanks for your reading this article, if this article is helpful for your project. So please comment on the same and share your friend’s circle.

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *