发送推送通知PHP

时间:2020-10-15 17:14:59

标签: javascript php firebase firebase-cloud-messaging

我有一个按钮:

<input type="text" id="userFCM" placeholder="Enter User fcmToken"><span><button onclick="sendNotification()">Send Notification</button>

我的功能:

function sendNotification() {
  var userFCM = document.getElementById('userFCM').value;
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.open("GET", "sendNotification.php?q="+userFCM, true);
  xmlhttp.send();
}

我的sendNotification.php:

<?php

$q = $_REQUEST["q"];

function sendPushNotification($to = '', $data = array()) {
   $apiKey = 'api key';
   $fields = array( 'to' => $to, 'notification' => $data );

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

   $url = 'https://fcm.googleapis.com/fcm/send';

   $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_VERIFYPEER, false);
   curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);
   curl_close($ch);
   return json_decode($result, true);
 }

 $data = array(
   'body' => 'test'
 );
 print_r(sendPushNotification($q, $data));

这似乎没有向设备发送推送通知,但是当我将值硬编码到sendNotification中并在没有按钮的情况下执行它时才起作用,但是当我传递值并单击按钮以发送时却没有

0 个答案:

没有答案