Send push notification to Android and IOS from PHP
Hello here is code for sending push notification in android and IOS
Php function for sending notification
                                   $message="Your message";
                                    $type='notification type';
                                    $extra=array();
                                    $extra['message']=$message;
                                    $extra['type']=$type;
                                    $extra['sound'] = 'default';
                                    $this->common->sendPushNotification($receiverUserId,$extra);
Send Notification in to android
 function sendNotificationToAndroid($deviceToken,$aps)
         {
             $apiKey = "4646464";    
                    $registrationIDs = array($deviceToken);
             $url = 'https://android.googleapis.com/gcm/send';
              $fields = array(
                                         'registration_ids' => $registrationIDs,
                                         'data' => $aps,
                                       );
 //echo "<pre>";print_r($fields);die; check what you get
                        $headers = array(
                                                      'Authorization: key=' . $apiKey,
                                                'Content-Type: application/json'
                                             );
                       //Open connection
                           $ch = curl_init();
                          //Set the url, number of POST vars, POST data
                          curl_setopt( $ch, CURLOPT_URL, $url );
                          curl_setopt( $ch, CURLOPT_POST, true );
                          curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
                          curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                          curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
                           //Execute post
                           $result = curl_exec($ch);
                          //echo "<pre>";print_r($result);
                           //Close connection
                           curl_close($ch);
                         If ($result === FALSE) {
                                die('Curl failed: ' . curl_error($ch));
                                  }
                                 return;
}
Send notification ti IOS
 function sendNotificationToIOS($deviceToken,$apn)
{
                     $passphrase = '';  
                     $ctx = stream_context_create();
                     stream_context_set_option($ctx, 'ssl', 'local_cert', FCPATH.'certificates/dev_cert.pem');  // your certificate path 
stream_context_set_option($ctx, 'ssl', 'passphrase','');     // passoword if you have created certificate with password else leave blank
 //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
     $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);// open this line when run with development profile
                  $body['aps']= $apn;
                  $payload = json_encode($body);
                 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
                  $result = fwrite($fp, $msg, strlen($msg));
                  fclose($fp);
                   return;    
}
Comments
Post a Comment