如何发送基于数据库字段的推送通知
问题描述:
我有一个应用程序使用普通的PhPMyAdmin数据库作为后端,应用程序与数据库的所有交互都通过PHP脚本进行。如何发送基于数据库字段的推送通知
现在我想基于数据库发送推送通知给用户。让我们说我有一个单独的数据库(用户名,要发送的消息)。
有任何服务或脚本可以让我做我想做的事吗?
或者是否有任何其他服务(例如Firebase)允许我做我想做的事?
答
在我们大多数人使用GCM进行推送通知之前,它已被弃用。我使用FCM(firebase云消息传递),它的工作令人惊叹。你永远不会去寻找GCM教程或指导了。这link会给你一切你需要设置推送通知。除此之外,如果您决定为ios FCM支持创建推送通知。如果您想创建聊天应用程序,请改用XMPP服务器。
有一些php代码从未在FCM文档中提及。在android中实现所有代码之后,您需要执行以下代码以将请求发布到FCM服务器,FCM将处理该代码并根据您提供的注册令牌推送到所有设备。
$url = 'https://fcm.googleapis.com/fcm/send';
//$field is the parameter you will be sending to FCM server in order to let FCM know which device, and the message you need to send to. You will get a better understading after study the doc.
$fields = array(
'priority' => "high",
'registration_ids' => $tokens,
'data' => $data
);
$headers = array(
'Authorization:key = 'your authorization key',
'Content-Type: application/json'
);
$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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
你会发现FCM的工作方式有点混乱,但最终你会习惯它。