2017-05-25 116 views
1

我需要做的是,我将从我的android应用中触发此脚本,设备所有者将收到通知。无法使用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; 


    ?> 

我读了很多博客,stackoverflows但没有阳性结果。

预先感谢您。

回答

0

您的PHP代码工作正常。其实你只发送data字段在$fields对象。请再次检查控制台是否有FCM数据。所以,你必须手动建立Notification对象。

但是,如果您想要通知面板的默认通知,请修改您的$fileds对象并运行。

$notificationObj = array(
       'title' => 'FCM', 
       'body' => 'MESSAGE' 
); 
$fields = array(
       'registration_ids' => $tokens, 
       'data' => $message, 
       'notification' => $notificationObj 
       ); 
相关问题