因為最近要從Firebase Cloud Message發出訊息通知,並要將發送的結果撈出,所以做了點筆記。免得以後忘記。
安裝官方版套件:
- 安裝:
在Laravel 安裝 官方版BigQuery套件 composer require google/cloud-bigquery
- 認證方式:
有兩種方式可以設定認證方式
use Google\Cloud\BigQuery\BigQueryClient;
// Authenticating with keyfile data.
$bigquery= new BigQueryClient([
'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);
// Authenticating with a keyfile path.
$bigquery= new BigQueryClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
另外還需要提供Google Cloud project ID才能往下做下去.
// Providing the Google Cloud project ID.
$bigquery= new BigQueryClient([
'projectId' => 'myProject'
]);
- 範例:
public function run_query(){
$bigQuery = new BigQueryClient([
'keyFilePath' => "../".env('GOOGLE_APPLICATION_CREDENTIALS'),
'projectId' => "project_id",
]);
// 统计消息指向的非重复应用实例(instance)的数量
$query = "SELECT COUNT(DISTINCT instance_id)
FROM `project_id.firebase_messaging.data`
WHERE
_PARTITIONTIME = TIMESTAMP('2020-10-27')
AND event = 'MESSAGE_ACCEPTED';";
$queryJobConfig = $bigQuery->query($query);
$queryResults = $bigQuery->runQuery($queryJobConfig);
foreach ($queryResults as $row) {
print_r($row);
}
}
將內容抓出後可能會看到一些欄位與相關的單詞,可以參考下面的表格內容:
- 參考網址: