应用程序到应用程序通知

vc9ivgsu  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(404)

我正在创建一个出租车应用程序,其中我有一个司机应用程序和一个用户应用程序。当用户发布乘车请求时,必须通知所有驾驶员请求可用(推送通知)。我的本机代码如何知道何时发送通知
我不希望应用程序需要大量的电池和数据,所以我不能每分钟都检查mysql数据库。我在找一个有firebase的解决方案,但发现他们提供web到android通知服务。我还没有写任何相关的代码。
请提出实现这一目标的方法。谷歌搜索也没用。如果可能,请附上代码。
提前谢谢

ibrsph3r

ibrsph3r1#

所以呢 firebase 不是很难使用,这里是你的代码 PHP Api ```
// function makes curl request to firebase servers
private function sendPushNotification($fields) {

require_once __DIR__ . '/config.php';

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

$headers = array(
    'Authorization: key=' . FIREBASE_API_KEY,
    'Content-Type: application/json'
);
// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}

// Close connection
curl_close($ch);

return $result;

}

现在config.php所拥有的是
你可以从我这里得到这把钥匙 `Firebase Console` 在“项目设置”->“云消息传递”->“服务器密钥”下
现在有一个简单的函数发送推送通知

public function send($to, $message) {
$fields = array(
'to' => $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}

在这里 `$to` 将提供您要发送通知的人的通知id。这是链接。
希望这对你有帮助

相关问题