2016-08-08 1 views
0

Dies ist mein PHP-Skript und ich sende den Körper und die Titel-Strings von meiner Android-App. Hier kann ich den Nachrichtentext in meiner Benachrichtigung, aber nicht den Titel und auch wenn ich setzen; $message = array( 'message' => $body, 'title' => 'This is my title', 'sound' => 1 );Wie kann ich den Titel meiner Push-Benachrichtigung in meinem PHP-Skript festlegen?

Dies funktioniert auch nicht.

<?php 
$body = $_POST["body"]; 
$title = $_POST["title"]; 
$inc_phone = $_POST["inc_phone"]; 

function send_notification ($tokens,$message) 
{ 
    $url = 'https://fcm.googleapis.com/fcm/send'; 
    $fields = array(
     'registration_ids' => $tokens, 
     'data' => $message 
     ); 
    $headers = array(
     'Authorization:key = AIzaSyAe9Qxx2URTyqXmf0mTheMq_ss_DOJaVQs ', 
     'Content-Type: application/json' 
     ); 
    $ch = curl_init(); 
    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_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
    $result = curl_exec($ch);   
    if ($result === FALSE) { 
     die('Curl failed: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 
//Connect to the database 
$host = "127.0.0.1"; 
$user = "rajhanssingh";      //Your Cloud 9 username 
$pass = "";         //Remember, there is NO password by default! 
$db = "fcm";         //Your database name you want to connect to 
$port = 3306;        //The port #. It is always 3306 

$conn = mysqli_connect($host, $user, $pass, $db, $port); 

//$conn = mysqli_connect("localhost","root","","fcm"); 
$sql = " Select token From utoken where phone_no='$inc_phone' "; 
$result = mysqli_query($conn,$sql); 
$tokens = array(); 
if(mysqli_num_rows($result) > 0){ 
    while ($row = mysqli_fetch_assoc($result)) { 
     $tokens[] = $row["token"]; 
    } 
} 
mysqli_close($conn); 
$message = array(
    'message' => $body, 
    'title' => $title, 
    'sound' => 1 
); 
$message_status = send_notification($tokens, $message); 
echo $message_status; 
?> 
+0

Hallo, Tarun. Konntest du es lösen? – JohnA10

Antwort

0

dies funktionierte für mich

function send_push_notification($registrationIDs, $message) { 

    $GOOGLE_API_KEY = "AIzaSyCzr6K_W3ImYwVQ-vQyhFztDSmchkFjeGQ"; 

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

    $fields = array(
     'registration_ids' => $registrationIDs, 
     'data' => ['message' => $message, 'title' => 'Title' ] 
    ); 

    $headers = array(
     'Authorization: key=' . $GOOGLE_API_KEY, 
     'Content-Type: application/json' 
    ); 




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

    // Disabling SSL Certificate support temporarly 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

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

    // Close connection 
    curl_close($ch); 
    // echo $result; 
} 
+1

ich sry, aber es hat nicht für mich funktioniert –

Verwandte Themen