Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:18:27+00:00 2026-06-14T10:18:27+00:00

I am implementing a Push Notification server for our iPhone application. I am using

  • 0

I am implementing a Push Notification server for our iPhone application.

I am using Symfony web framework to build my backend system for my iPhone application.

The way I have built the push notification server is:

1) Make a socket stream connection to Apple’s PNS.

2) Start an infinite while loop

3) Inside the while loop, look for any new notifications in the SQL database

4) If there are new push notification entities in my SQL database, get them all and send them out

Below is my PHP code:

// ----------------------------------------------------------
// Opens a connection to Apple's Push Notification server
// ----------------------------------------------------------
public function pnsConnect()
{
    // push notification pem certificate file
    //$this->apnscert_dev = $this->container->getParameter('apnscert_dev');
    //$this->apnshost_dev = $this->container->getParameter('apnshost_dev');
    $this->apnscert_dev = 'cert_and_key_dev.pem';
    $this->apnshost_dev = 'gateway.sandbox.push.apple.com';
    $this->apnsport = 2195; //$this->container->getParameter('apnsport');

    $pempath_dev = __DIR__."/../Resources/config/".$this->apnscert_dev;

    echo 'pem path = '.$pempath_dev.'<br />';

    $streamContext = stream_context_create();

    stream_context_set_option($streamContext, 'ssl', 'local_cert', $pempath_dev);

    // push notification server connection object
    $this->apns = stream_socket_client('ssl://' . $this->apnshost_dev . ':' . $this->apnsport, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
    error_log(date('Y-m-d H:i:s')." - Successfully connected to APNS", 3, 'PushLog.log'); // log for successful connection to help with debugging
}


// ---------------------------------------------------------
// Sends a push notification to multiple targeted tokens
// i.e. a group of users
//
// pnsSendToTokenGroup() only sends to a group, not a list
// of tokens specifically selected
// ---------------------------------------------------------    
public function pnsSendToMultiple($paramArrTokens, $paramMessage)
{
    if(!$paramMessage || !$paramArrTokens)
    {
        return new Response('Missing input parameters');
    }

    $badge = 1;
    $sound = 'default';
    $development = true;

    $payload = array();
    $payload['aps'] = array('alert' => $paramMessage, 'badge' => intval($badge), 'sound' => $sound);
    $payload = json_encode($payload);

    //echo 'message = '.$paramMessage.'<br />';

    //echo '<br />Received '.count($paramArrTokens).' tokens<br />';

    foreach($paramArrTokens as $paramToken)
    {
        //echo 'current token = '.$paramToken.'<br />';

        $apns_message = chr(0).chr(0).chr(32).pack('H*', str_replace(' ', '', $paramToken)).chr(0).chr(strlen($payload)).$payload;
        fwrite($this->apns, $apns_message);

        $apns_message = null;
        //$paramToken = null;
    }

    $badge = null;
    $sound = null;
    $development = null;
    $payload = null;

    //$paramArrTokens = null;
    //$paramMessage = null;
}


// ---------------------------------------------------------
// Keeps this PNS server alive to maintain the connection
// to Apple's PNS server. This process will continually
// check the database for any new notification and push
// the notificaiton to who ever we need to.
// ---------------------------------------------------------    
public function pnsKeepAlive()
{
    // prevents time out
    ini_set('max_input_time', 0);
    ini_set('max_execution_time', 0);

    $this->pnsDisconnect();
    $this->pnsConnect();

    // circular reference collector
    gc_enable();

    // start infinite loop to keep monitoring for new notifications
    while(true)
    {
        echo 'checking database for new notifications ...<br />';

        set_time_limit(0);

        gc_collect_cycles();

        $today = new DateTime('today');
        $now = new DateTime();

        $query = $this->em->createQuery('SELECT n FROM MyAppWebServiceBundle:Notification n WHERE n.sent = :paramSent and n.notificationdate >= :paramDate and n.notificationdate <= :paramNow')
            ->setParameter('paramSent', false)
            ->setParameter('paramDate', $today)
            ->setParameter('paramNow', $now);

        $notifications = $query->getResult();

        $today = null;
        $now = null;

        //echo 'number of unsent notifications found for today = '.count($notifications).'<br />';

        // for each notification, combine all tokens into a single array
        // to be looped through and sent into notification processing queue
        foreach($notifications as $notification)
        {
            $this->pushNotification($notification);
        }

        $this->em->detach($query);

        $notifications = null;
        $query = null;
    }

    $this->pnsDisconnect();
}




// ---------------------------------------------------------    
// Finds all tokens attached to Notification
// and construct an array of all unique tokens to be
// sent out to all receivers of the notification
// ---------------------------------------------------------    
public function pushNotification($notification)
{
    // initialise an empty array
    $arrAllTokens = array();

    // add all raw tokens to final array first
    foreach($notification->getTokens() as $token)
    {
        // only add active tokens
        if($token->getActive() == true)
        {
            $arrAllTokens[] = $token->getToken();
        }
    }

    // for each token group add all 
    // tokens in each group to final array
    foreach($notification->getTokenGroups() as $tokenGroup)
    {
        foreach($tokenGroup->getTokens() as $token)
        {
            // only add active tokens
            if($token->getActive() == true)
            {
                $arrAllTokens[] = $token->getToken();
            }
        }
    }

    $arrAllTokens = array_unique($arrAllTokens);

    $this->pnsSendToMultiple($arrAllTokens, $notification->getMessage());

    $notification->setSent(true);
    $this->em->flush();
    $this->em->detach($notification);

    $arrTokens = null;
    $arrAllTokens = null;
    $notification = null;
}

Apple have stated that a Push Provider should maintain connection to Apple’s Push Server and not frequently disconnection and reconnect, otherwise they treat it as a denial of service attack and block us.

That is why I have an infinite while loop so the script doesn’t end to allow me to maintain the constant connection to Apple’s push notification socket stream.

At the moment, my push notification server works on my local machine (my pnsKeepAlive() infinite while loop goes on forever) but when I deploy my code to the production server, my pnsKeepAlive() (see above code) does not go on forever.

My production server is a shared hosting on Linode. It is a LAMP server running Apache and Debian.

I have heard PHP isn’t designed to be doing these kind of jobs.

So my questions whether there are other language that is designed for these kind of thing (maintaining a persistent connection for a push notification server).

I have looked into Gearman but was also told by others Gearman isn’t really what I need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:18:28+00:00Added an answer on June 14, 2026 at 10:18 am

    I have had some good stories about Node JS + socket.io for push notification servers.

    Here is a great article, which may help you: http://www.gianlucaguarini.com/blog/nodejs-and-a-simple-push-notification-server/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a push notification in my application. Server: php Client: iphone Sever
while i'm using push notification in iphone with local server(used php), not able to
I'm implementing an iPhone application that syncs data with an AppEngine backend. I'm using
I'm implementing an windows phone 7 application with push notification. The push notification would
I am interested in implementing C2DM for the push notification feature in my application.
I am implementing C2DM for push notification in an android application, it need at
I was implementing the server-push content using comet to the client browser. There should
In my application I am implementing the Push Notification Service. I have a Content
I am implementing Push Notification using Urban Airship library. I am able to receive
I'm implementing push notification and i'm managing it when user is using app. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.