2015-08-20 11 views
8

मैंने php और mysql का उपयोग कर एक वेबसाइट विकसित की है। मैं अपनी वेबसाइट पर अपलोड की गई प्रत्येक पोस्ट के लिए एंड्रॉइड डिवाइस पर अधिसूचना भेजना चाहता हूं।PHP से एंड्रॉइड को अधिसूचना कैसे भेजें?

क्या जीसीएम सर्वर का उपयोग कर अधिसूचना भेजना संभव है? यदि हां, तो मैं एंड्रॉइड ऐप इंस्टॉल करने वाले सभी उपकरणों पर अधिसूचना कैसे भेज सकता हूं?

+0

यहाँ आप कर रहे हैं [GitHub पर Google क्लाउड संदेश सेवा] (https://github.com/google/gcm के लिए उत्तर पोस्ट का फैसला किया है) – BNK

उत्तर

2

मैं अपने ही सवाल

अब गूगल की शुरुआत की FCM

<?php 
define('API_ACCESS_KEY','Api key from Fcm add here'); 
$fcmUrl = 'https://fcm.googleapis.com/fcm/send'; 
$token='235zgagasd634sdgds46436'; 

/ $notification = [ 
      'title' =>'title', 
      'body' => 'body of message.', 
      'icon' =>'myIcon', 
      'sound' => 'mySound' 
     ]; 
     $extraNotificationData = ["message" => $notification,"moredata" =>'dd']; 

     $fcmNotification = [ 
      //'registration_ids' => $tokenList, //multple token array 
      'to'  => $token, //single token 
      'notification' => $notification, 
      'data' => $extraNotificationData 
     ]; 

     $headers = [ 
      'Authorization: key=' . API_ACCESS_KEY, 
      'Content-Type: application/json' 
     ]; 


     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$fcmUrl); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification)); 
     $result = curl_exec($ch); 
     curl_close($ch); 


     echo $result; 
12

सबसे पहले Implementing GCM Client का पालन करें और अपने एंड्रॉइड ऐप में जीसीएम क्लाइंट को लागू करें।

जीपीएम सर्वर के लिए PHP में आप इसे निम्न तरीके से कार्यान्वित कर सकते हैं। पोस्ट को प्रकाशित करते समय कोड की तरह अपनी आवश्यकता के अनुसार अपना कोड संपादित करें। GCM सर्वर को लागू करने के बारे में अधिक जानकारी के लिए जाना Implementing GCM Server

<?php 
    // Replace with the real server API key from Google APIs 
    $apiKey = "your api key"; 

    // Replace with the real client registration IDs 
    $registrationIDs = array("reg id1","reg id2"); 

    // Message to be sent 
    $message = "Your message e.g. the title of post"; 

    // Set POST variables 
    $url = 'https://android.googleapis.com/gcm/send'; 

    $fields = array(
     'registration_ids' => $registrationIDs, 
     'data' => array("message" => $message), 
    ); 
    $headers = array(
     'Authorization: key=' . $apiKey, 
     'Content-Type: application/json' 
    ); 

    // Open connection 
    $ch = curl_init(); 

    // Set the URL, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    // curl_setopt($ch, CURLOPT_POST, true); 
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    // Execute post 
    $result = curl_exec($ch); 

    // Close connection 
    curl_close($ch); 
    // print the result if you really need to print else neglate thi 
    echo $result; 
    //print_r($result); 
    //var_dump($result); 
?> 

को भी शुरुआती के लिए एक अच्छा पोस्ट Android Push Notifications नहीं है।

+0

मुझे लगता है कि प्रश्न – Amy

+0

में कोई PHP टैग नहीं है यह बहुत अच्छा काम करता है! हालांकि, मुझे आश्चर्य है कि क्या होता है यदि Google इस यूआरएल को बदलता है: 'https: // android.googleapis.com/gcm/send'? वह कितनी संभावना है? यदि संभव हो, तो क्या कोई PHP लाइब्रेरी है जो परिवर्तनों को बनाए रखेगी, अगर मैं इसे बदलता हूं तो मैं अपने ऐप में मैन्युअल रूप से उस यूआरएल को बदलना नहीं चाहता हूं। – Taurus

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