Skip to content

ChouBetter/WebPush

Repository files navigation

WebPush

Knowledge

Implement - PushCodelab

Implement - Firebase Cloud Messaging

<?php

define('API_SERVER_ACCESS_KEY', '伺服器金鑰');

$token = ''; /* 接收端的token */
$message = ''; /* 內容 */
$title = ''; /* 標題 */

$content = array
    (
    'title' => $title,
    'body' => $message
);

$fields = array
    (
    'to' => $token,
    'notification' => $content
);

$headers = array
    (
    'Authorization: key=' . API_SERVER_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;