【问题标题】:Gmail Push Notification using PHP使用 PHP 的 Gmail 推送通知
【发布时间】:2017-08-14 11:22:24
【问题描述】:
【问题讨论】:
标签:
php
push-notification
gmail
gmail-api
google-cloud-pubsub
【解决方案2】:
使用 HTTP POST 的代码示例:
// Google API
$client = getClient();
// Variables
$user = 'me';
$access_token = $client->getAccessToken()['access_token'];
$topic_name = 'projects/xxxx/topics/xxxx';
// POST request
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/' . $user . '/watch');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $access_token,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode(array(
'topicName' => $topic_name,
'labelIds' => ["INBOX"]
))
));
to grant publish privileges 到 serviceAccount:gmail-api-push@system.gserviceaccount.com 是必需的。