如何使用PHP发送FCM通知? [英] How to send FCM notification using PHP?
本文介绍了如何使用PHP发送FCM通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用PHP代码/网络浏览器发送FCM.
I tried to send FCM using PHP code/web browser.
但是问题是当我使用PHP Web浏览器发送它时
But the problem is when I send it using PHP web browser:
- FCM通知仅显示在虚拟设备上.
- FCM通知未出现在真实的电话设备上.
而且我只能使用Firebase控制台将FCM通知发送到真实的电话设备.
And I can only send FCM notifications to real phone devices using Firebase Console.
有人可以帮忙吗?代码如下.
Can somebody help? The code is below.
<?php
require "init.php";
global $con;
if(isset($_POST['Submit'])){
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AAAA2gV_U_I:APA91bHA28EUGmA3BrDXFInGy-snx8wW6eZ_RUE7EtOyM99pbfrVZU_ME-FU0O9_dUxYpM30OYF8KWYlixod_PfwbgLNoovzdkdJ4F-30vY8X_tBz0CMrajCIAgbNVRfw203YdRGli";
$sql = "SELECT fcm_token FROM fcm_table";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_row($result);
$key = $row[0];
$headers = array('Authorization:key=' .$server_key, 'Content-Type:application/json');
$fields = array('to' => $key, 'notification' => array('title' => $title, 'body'=> $message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>FCM Notification</title>
</head>
<body>
<form action='fcm_notification.php' method="POST">
<table>
<tr>
<td>Title : </td>
<td><input type="text" name="title" required="required" /></td>
</tr>
<tr>
<td>Message : </td>
<td><input type="text" name="message" required="required" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Send notification"></td>
</tr>
</table>
</form>
</body>
</html>
谢谢.
推荐答案
通过以下方式,您可以使用Google FCM将推送通知发送到移动设备.对我来说,它的作品符合预期.添加键'priority' => 'high'
By the following way you can send push notification to mobile using google FCM. For me its works as expected. Add the key 'priority' => 'high'
function sendPushNotification($fields = array())
{
$API_ACCESS_KEY = 'YOUR KEY';
$headers = array
(
'Authorization: key=' . $API_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 );
return $result;
}
$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
'registration_ids' => ['deviceID'],
'data' => '',
'priority' => 'high',
'notification' => array(
'body' => $message,
'title' => $title,
'sound' => 'default',
'icon' => 'icon'
)
);
sendPushNotification($fields);
这篇关于如何使用PHP发送FCM通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文