2015-08-20 14 views
8

Ich habe eine Website mit PHP und Mysql entwickelt. Ich möchte eine Benachrichtigung an Android-Gerät für jeden Beitrag senden, der auf meiner Website hochgeladen wird.Wie kann ich Benachrichtigungen von php an Android senden?

Ist es möglich, Benachrichtigungen über GCM Server zu senden? Wenn ja, wie kann ich Benachrichtigungen an alle Geräte senden, auf denen die Android App installiert ist?

+0

Hier sind Sie [Google Cloud Messaging bei GitHub] schreiben (https://github.com/google/gcm) – BNK

Antwort

2

Ich habe beschlossen Antwort für meine eigene Frage

Jetzt Google FCM eingeführt

<?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

Zuerst folgen Sie Implementing GCM Client und implementieren Sie den gcm-Client in Ihrer Android-App.

Für GCM Server in PHP können Sie es auf folgende Weise implementieren. Bearbeiten Sie Ihren Code nach Ihren Bedürfnissen wie Code für die Veröffentlichung der Post. Für mehr über die GCM Server Implementierung gehen zu 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); 
?> 

Es gibt auch Android Push Notifications für Anfänger ein guter Beitrag ist.

+0

Ich denke, es gibt kein PHP-Tag in Frage – Amy

+0

Das hat funktioniert, großartig! Ich frage mich jedoch, was passiert, wenn Google diese URL ändert: https: // android.googleapis.com/gcm/send? Wie wahrscheinlich ist das? Wenn es wahrscheinlich PHP-Bibliotheken gibt, die mit den Änderungen Schritt halten, möchte ich diese URL in meiner App nicht manuell ändern, falls Google sie ändert. – Taurus

Verwandte Themen