4

iPhone に通知を送信する PHP スクリプトがあります (以下)。Web サイトのコードは C# です。私がやりたいことは、C# から PHP スクリプトに情報を渡すことです。

PHP スクリプト

<?php

// Put your device token here (without spaces):
$deviceToken = ''; //Get from C#

// Put your private key's passphrase here:
$passphrase = ''; //Get from C#

// Put your alert message here:
$message = 'New Message';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
                           'ssl://gateway.push.apple.com:2195', $err,
                           $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
                     'alert' => $message,
                     'sound' => 'default'
                     );

// 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));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

?>

このスクリプトに deviceToken とパスフレーズを渡したいと思います。すべてが同じサーバー上にあり、サーバー上の同じ場所にあります。

PHP スクリプトを開始する C# コードはこちらです。このコードは基本的に、通知を送信する必要があるすべてのデバイストークンを取得しています。その foreach ループの内側で、PHP スクリプトを呼び出す必要があります。

    private void SendAppleNotifications(List<NotificationInfo> AppleNotifications)
    {
        ApplePushNotification push = new ApplePushNotification(false, AppleCertificate, ApplePassword);

        List<NotificationPayload> notificationList = new List<NotificationPayload>();


        List<string> returnStrings = new List<string>();

        foreach (NotificationInfo ni in AppleNotifications)
        {                      
        }

        returnStrings = push.SendToApple(notificationList);
    }

どんな助けでも大歓迎です。ありがとう

4

1 に答える 1

3

PHP スクリプトで $_POST を使用して deviceToken とパスフレーズを取得します

<?php

// Put your device token here (without spaces):
$deviceToken = $_POST['deviceToken'];
// Put your private key's passphrase here:
$passphrase = $_POST['passphrase'];

?>

PHP サイトにデータを投稿する C# メソッド

public string SendPost(string url, string postData)
{
    string webpageContent = string.Empty;

    try
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = byteArray.Length;

        using (Stream webpageStream = webRequest.GetRequestStream())
        {
            webpageStream.Write(byteArray, 0, byteArray.Length);
        }

        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
            {
                webpageContent = reader.ReadToEnd();
            }
        }
    }
    catch (Exception ex)
    {
        //throw or return an appropriate response/exception
    }

    return webpageContent;
}

そして最後にこのメソッドを呼び出します

String deviceToken = HttpUtility.UrlEncode("YourDeviceToken");
String passphrase = HttpUtility.UrlEncode("YourPassphrase");

SendPost("http://yourphpsite.com/xxx.php", String.Format("deviceToken={0}&passphrase={1}", deviceToken, passphrase));
于 2013-03-05T15:42:11.597 に答える