2017-05-25 10 views
1

私がしなければならないのは、私はアンドロイドアプリからこのスクリプトを起動し、デバイスの所有者に通知を受け取ります。私はブログ、stackoverflowsしかし、誰肯定的な結果をたくさん読みましたphpを使用してfirebaseのクラウドメッセージングを起動できません

<?php 
    $db_name = "testdb"; 
    $mysql_username = "root"; 
    $mysql_password = ""; 
    $server_name = "localhost"; 

    function send_notification ($tokens , $message) 
    { 
$url = 'https://fcm.googleapis.com/fcm/send'; 
$fields = array(
       'registration_ids' => $tokens, 
       'data' => $message 
       ); 
$headers = array(
       'Authorization:key = AIzaSyBAYIZohaZPXBEGqktPh8YEfMlmuYnuCOk', 
       '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; 
    } 

$conn = mysqli_connect  
        ($server_name,$mysql_username,$mysql_password,$db_name); 
$sql = "select `token` from `user_token`;"; 
$result = mysqli_query($conn,$sql); 
$token = array(); 
if(mysqli_num_rows($result) > 0){ 
    while($row = mysqli_fetch_assoc($result)){ 
     $token[] = $row["token"]; 
    } 
} 
mysqli_close($conn); 
$message = array('message' => "FCM MESSAGE"); 
$message_status = send_notification($token , $message); 
echo $message_status; 


    ?> 

は、ここに私のコードです。

ありがとうございます。

答えて

0

あなたのPHPコードは正常に動作しています。実際にdataフィールドは$fieldsオブジェクトにのみ送信しています。 FCMデータについてもう一度コンソールを確認してください。したがって、手動でNotificationオブジェクトを構築する必要があります。

ただし、通知パネルへのデフォルトの通知を希望する場合は、$filedsオブジェクトを次のように変更して実行してください。

$notificationObj = array(
       'title' => 'FCM', 
       'body' => 'MESSAGE' 
); 
$fields = array(
       'registration_ids' => $tokens, 
       'data' => $message, 
       'notification' => $notificationObj 
       ); 
関連する問題