2012-05-03 14 views
6

मेरे पास आईफोन और एंड्रॉइड के लिए एक ऐप है। मैं एएसपीनेट webservice का उपयोग कर अपने ऐप में कस्टम पुश अधिसूचना को कार्यान्वित करना चाहता हूं।एएसपीनेट webservice का उपयोग कर कस्टम पुश अधिसूचना कैसे बनाएं?

मैं यह कैसे कर सकता हूं?

क्या कोई नमूना कोड है जो webservice के साथ डेटा का प्रबंधन करता है? कृपया मुझे सुझाव दें कि मैं इसके लिए webservice का प्रबंधन कैसे कर सकता हूं।

धन्यवाद।

उत्तर

2

नीचे कोड PHP के लिए है।

ऐसा कोई कोड आसानी से उपलब्ध नहीं है। आप इसे डेटाबेस तालिका में 1 टैग फ़ील्ड बनाए रखने जैसे आईओएस डिवाइस के लिए 1 और एंड्रॉइड डिवाइस के लिए 2 कर सकते हैं। मैंने समान कार्यक्षमता लागू की है और तदनुसार पुश अधिसूचना भेजने के लिए कोड है।

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

नोट: कोई शक/क्वेरी के मामले में संपर्क करने के लिए स्वतंत्र महसूस। टिप्पणी छोड़ दो

+0

उत्तर के लिए धन्यवाद। मेरे पास कुछ प्रश्न हैं क्योंकि मैं इसके लिए नया हूं। क्या इसे लागू करने के लिए कोई फ्रेमवर्क आवश्यक है? और क्या मैं सीधे अपने वेब सेवा में अपना कोड उपयोग कर सकता हूं या मुझे इसके लिए बदलाव करना होगा। – Developer

+0

आप सीधे अपना कोड जोड़ सकते हैं लेकिन आपको चयन क्वेरी में बदलाव करने की आवश्यकता है। आपको अपनी कॉन्फ़िगरेशन फ़ाइल में कुछ स्थिरांक घोषित करने की आवश्यकता है। मैं क्षमा चाहता हूं लेकिन यह कोड PHP के लिए है। प्रतीक्षा करें जबकि आपको आईओएस पुश अधिसूचना के लिए भी .NET के लिए समाधान मिलेगा। –

+0

पुश अधिसूचना के लिए नेट आधारित कोड के लिए मेरा अन्य उत्तर देखें। –

3

कृपया नेट पुश अधिसूचना के लिए निम्नलिखित code from github उपलब्ध का उपयोग करें। आपको इसमें .p12 फ़ाइल प्रदान करने की आवश्यकता है और आपको अपनी आवश्यकता के अनुसार डेवलपर/उत्पादन मोड सेट करना होगा।

+0

क्या ऐसा है? मुझे हमारे समुदाय पर कहीं भी इस तरह का नियम नहीं मिला है। –

+0

आप यह देखने के इच्छुक हो सकते हैं http://meta.stackexchange.com/questions/25209/what-is-the-official-etiquette-on-answering-a-question-twice –

+0

जेफ एटवुड का संदर्भ लें (जो एक है मॉडरेटर) लांस रॉबर्ट के जवाब के नीचे टिप्पणी करें और जेफ का जवाब स्वयं ही बताता है। –

संबंधित मुद्दे