Android 在配置文件中声明几个常量。我同意,但这段代码是为PHP编写的。请稍候,我将为您提供用于iOS推送通知的.net解决方案。请检查我的另一个答案,了解用于推送通知的基于.net的代码。谢谢您的回答。我将尝试实现您的代码,如果有任何查询,我将询问您。嘿,正如您所

Android 在配置文件中声明几个常量。我同意,但这段代码是为PHP编写的。请稍候,我将为您提供用于iOS推送通知的.net解决方案。请检查我的另一个答案,了解用于推送通知的基于.net的代码。谢谢您的回答。我将尝试实现您的代码,如果有任何查询,我将询问您。嘿,正如您所,android,iphone,asp.net,web-services,push-notification,Android,Iphone,Asp.net,Web Services,Push Notification,在配置文件中声明几个常量。我同意,但这段代码是为PHP编写的。请稍候,我将为您提供用于iOS推送通知的.net解决方案。请检查我的另一个答案,了解用于推送通知的基于.net的代码。谢谢您的回答。我将尝试实现您的代码,如果有任何查询,我将询问您。嘿,正如您所说,我需要在配置文件中声明几个常量。是否需要.net webservice还有?是这样吗?我在我们社区的任何地方都找不到这样的规则。您可能有兴趣查看这一点,请参考Jeff Atwood(他是主持人)在Lance Robert的答案下面的评论,J


在配置文件中声明几个常量。我同意,但这段代码是为PHP编写的。请稍候,我将为您提供用于iOS推送通知的.net解决方案。请检查我的另一个答案,了解用于推送通知的基于.net的代码。谢谢您的回答。我将尝试实现您的代码,如果有任何查询,我将询问您。嘿,正如您所说,我需要在配置文件中声明几个常量。是否需要.net webservice还有?是这样吗?我在我们社区的任何地方都找不到这样的规则。您可能有兴趣查看这一点,请参考Jeff Atwood(他是主持人)在Lance Robert的答案下面的评论,Jeff的答案本身也解释了这一点。这个答案可能对其他正在寻找PHP解决方案的人有用,我相信不会被删除。如果你仍然希望被删除,请提醒版主注意,让他决定如何处理。没问题。别管了。我的兴趣是不要删除你的答案。但是如果你能把答案合并起来,那就太好了。没什么私事
//This function determines which is the device and send notification accordingly.
function sendPushNotificaitonToAllUser($NotificationMsg)
{
    $sql = "select ID,DeviceToken,DeviceType from TblDeviceToken";
    $rs  = mysql_query($sql);
    $num = mysql_num_rows($rs);

    if($num >= 1)
    {
        while($row = mysql_fetch_array($rs))
        {
            $deviceToken = $row['DeviceToken'];
            if($deviceToken!='' || $deviceToken!='NULL')
            {
                if($row['DeviceType']==1)   
                    deliverApplePushNotification($deviceToken,$NotificationMsg);
                else if($row['DeviceType']==2)  
                    sendAndroidPushNotification($deviceToken,$NotificationMsg);
            }
        }
    }     
}

//APPLE PUSH NOTIFICATION DELIVERY
function deliverApplePushNotification($deviceToken,$message)
{
    //Create context
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', PEM_FILE_NAME);
    stream_context_set_option($ctx, 'ssl', 'passphrase', APPLE_PEM_PASSPHRASE);

    //Establish connection
    $fp = stream_socket_client(APPLE_URL, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

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

    $body['aps'] = array('alert' => $message,   'sound' => 'default'); // Create the payload body
    $payload = json_encode($body); // Encode the payload as JSON
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;// Build the binary notification
    $result = fwrite($fp, $msg, strlen($msg));// Send it to the server


    //If want to keep track of delivery can be done from here.
    /*if (!$result)
        echo '<br/>Message not delivered-->$deviceToken' . PHP_EOL;
    else
        echo '<br/>Message successfully delivered-->$deviceToken' . PHP_EOL;        
    */

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

function sendAndroidPushNotification($deviceRegistrationId,$messageText)
{   
    $authenticationID=googleAuthenticate(ANDROID_USERNAME,ANDROID_PASSWORD,ANDROID_SOURCE,ANDROID_SERVICE);
    $result= sendMessageToPhone($authenticationID,$deviceRegistrationId,ANDROID_MSGTYPE,$messageText);
}   

function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") 
{    
    session_start();
    if( isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null)
        return $_SESSION['google_auth_id'];

    // get an authorization token
    $ch = curl_init();
    if(!ch){
        return false;
    }

    curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
    $post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
        . "&Email=" . urlencode($username)
        . "&Passwd=" . urlencode($password)
        . "&source=" . urlencode($source)
        . "&service=" . urlencode($service);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);    
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // for debugging the request
    //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request

    $response = curl_exec($ch);

    //var_dump(curl_getinfo($ch)); //for debugging the request
    //var_dump($response);

    curl_close($ch);

    if (strpos($response, '200 OK') === false) {
        return false;
    }

    // find the auth code
    preg_match("/(Auth=)([\w|-]+)/", $response, $matches);

    if (!$matches[2]) {
        return false;
    }

    $_SESSION['google_auth_id'] = $matches[2];
    return $matches[2];
}

function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) 
{
    $headers = array('Authorization: GoogleLogin auth=' . $authCode);
    $data = array(
                            'registration_id' => $deviceRegistrationId,
                            'collapse_key' => $msgType,
                            'data.message' => $messageText //TODO Add more params with just simple data instead           
                        );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
    if ($headers)
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
}