如何设置Gmail推送通知

时间:2020-03-05 15:31:52

标签: push-notification gmail gmail-api google-cloud-pubsub

一天以来,我一直试图了解this的工作方式,现在我完全感到困惑。

这是我的目标(非常简单):当我收到新电子邮件时,对URL进行GET请求。

  • 我按照他们的要求创建了一个 topic (名为 new-email
  • 我在该主题中有一个 subscription (名为 new-email-inbox ),交付类型为 push ,并设置了我的端点URL 。
  • 我已向该帐户授予订阅授权: gmail-api-push@system.gserviceaccount.com Editor Editor Pub / Sub >)

如何设置其余部分?我不知道现在该怎么办。

1 个答案:

答案 0 :(得分:1)

我执行了一个 Symfony4 命令,该命令每天像cron一样执行:

class WatchGoogleClient extends Command {

    private $kernel;
    private $gmailService;

    public function __construct(GmailService $gmailService, Kernel $kernel)
    {
        parent::__construct();
        $this->gmailService = $gmailService;
        $this->kernel = $kernel;
    }

    protected function configure()
    {
        $this->setName('app:watch-google-client')
            ->setDescription('watch-google-client')
            ->setHelp('Reset the google watch timer');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // This getClient function is basically what is given by the google API tutorial
        $client = $this->gmailService->getClient($this->kernel);
        $service = new \Google_Service_Gmail($client);
        $watchreq = new \Google_Service_Gmail_WatchRequest();
        $watchreq->setLabelIds(array('INBOX'));
        $watchreq->setTopicName('YOUR_TOPIC_NAME');
        $msg = $service->users->watch('me', $watchreq);

        var_dump($msg);
        // DO WHAT YOU WANT WITH THIS RESPONSE BUT THE WATCH REQUEST IS SET
    }
}