This post will help you to send push notification to iPhone devices using server side scripting language called php.
Here are code to send push notification
/**
     * Apple Push Notification Services (APNS)
     *
     * @param   array   data
     * @param   string  message
     * @param   array   custom_fields
     * @return  string  status  success|failure
     */
    function iphonePushNotification( $data = array(), $message = NULL, $custom_fields = array() )
    {
        $message = !empty($message) ? trim($message) : '';
        if ( count($data)==0 )
            return 'empty device token.';
        if ( empty($message) )
            return 'empty message.';
        // Set private key's passphrase & local cert file path
        $passphrase = PASSPHRASE;
        $local_cert = LOCAL_CERT_PATH;
        $streamContext = stream_context_create();
        stream_context_set_option($streamContext, 'ssl', 'local_cert', $local_cert);
        stream_context_set_option($streamContext, 'ssl', 'passphrase', $passphrase);
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 20, STREAM_CLIENT_CONNECT, $streamContext); // Open a connection to the APNS server
        if (!$fp)
        {
            $status = "failure";
        }
        else 
        {
            $c = count($data);
            for ( $i=0; $i<$c; $i++ )
            {
                // iPhone Device Token Without Any Spaces
                $deviceToken = isset($data[$i]['device_token']) && !empty($data[$i]['device_token']) ? trim($data[$i]['device_token']) : '';
                $deviceToken = str_replace(' ', '', $deviceToken);
                // Set badge
                $badge = isset($data[$i]['badge']) ? $data[$i]['badge'] : 1;
                // Check if device token not empty
                if ( !empty($deviceToken) )
                {
                    // Create the payload body
                    $body['aps'] = array(
                        'alert'         =>  $message,
                        'badge'         =>  $badge,
                        'sound'         =>  'default',
                        'custom_fields' =>  $custom_fields
                    );
                    // Encode the payload as JSON
                    $payload = json_encode($body);
                    // Build the binary notification
                    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
                    // Send it to the server
                    $result = fwrite($fp, $msg, strlen($msg));
                }
            }
            $status = 'success';
        }
        fclose($fp);
        return $status;
    }
                       
                    
0 Comment(s)