2017-09-14 103 views
1

我正在构建一个PHP网站。我想将通知推送给客户端(登录到我的网站),因此我使用Firebase云消息传递。如何通过PHP获取Firebase目标?

我已经注册火力地堡的应用程序和编写一个函数来发送消息

public function sendMessage($data, $target) 
{ 
    //FCM api URL 
    $url = 'https://fcm.googleapis.com/fcm/send'; 
    //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key 
    $server_key = 'MY SERVER KEY'; 

    $fields = array(); 
    $fields['data'] = $data; 
    if (is_array($target)) { 
     $fields['registration_ids'] = $target; 
    } else { 
     $fields['to'] = $target; 
    } 
    //header with content_type api key 
    $headers = array(
     'Content-Type:application/json', 
     'Authorization:key=' . $server_key 
    ); 

    $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('FCM Send Error: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

但我不知道怎么去$target让火力地堡发送消息。

enter image description here

现在我想,当用户注册/我的网站上登录,我会要求从火力地堡token并将其保存到我的数据库。

因为当我想要向该用户发送消息时,我需要为Firebase提供目标(令牌)。任何人都请帮助我?

回答

0

起初,客户端应该有使用您的Firebase项目的应用程序。 然后你可以沙子消息给它的应用程序,你有2种方法来做到这一点:

  • 你要获得他的注册标记
  • 他对智能手机应用程序应该在某些话题进行注册,恩。 “一般”。
+0

我不知道你到底在说什么。我只想在用户在我的网站上注册时从Firebase获取用户令牌。当然,我的网站的标记和firebase的标记是不同的 – Jusfunny