我从新的谷歌通知服务Firebase Cloud Messaging开始。
多亏了这段代码https://github.com/firebase/quickstart-android/tree/master/messaging,我才能从我的Firebase用户控制台发送通知到我的Android设备。
有没有什么API或方法可以在不使用Firebase控制台的情况下发送通知?我的意思是,例如,一个PHP API或类似的东西,直接从我自己的服务器创建通知。
我从新的谷歌通知服务Firebase Cloud Messaging开始。
多亏了这段代码https://github.com/firebase/quickstart-android/tree/master/messaging,我才能从我的Firebase用户控制台发送通知到我的Android设备。
有没有什么API或方法可以在不使用Firebase控制台的情况下发送通知?我的意思是,例如,一个PHP API或类似的东西,直接从我自己的服务器创建通知。
当前回答
使用curl的示例
向特定设备发送消息
要将消息发送到特定设备,请将该设置为特定应用实例的注册令牌
curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>" -X POST -d '{ "data": { "score": "5x1","time": "15:10"},"to" : "<registration token>"}' https://fcm.googleapis.com/fcm/send
向主题发送消息
这里的主题是:/topics/foo-bar
curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>" -X POST -d '{ "to": "/topics/foo-bar","data": { "message": "This is a Firebase Cloud Messaging Topic Message!"}}' https://fcm.googleapis.com/fcm/send
向设备组发送消息
向设备组发送消息与向单个设备发送消息非常相似。将to参数设置为设备组的唯一通知键
curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>" -X POST -d '{"to": "<aUniqueKey>","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send
使用服务API的示例
火网址:https://fcm.googleapis.com/fcm/send
头
Content-type: application/json
Authorization:key=<Your Api key>
请求方式:POST
请求体
发送到特定设备的消息
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to": "<registration token>"
}
主题的消息
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
给设备组的消息
{
"to": "<aUniqueKey>",
"data": {
"hello": "This is a Firebase Cloud Messaging Device Group Message!"
}
}
其他回答
2020年作品
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization'=> 'key='. $token,
])->post($url, [
'notification' => [
'body' => $request->summary,
'title' => $request->title,
'image' => 'http://'.request()->getHttpHost().$path,
],
'priority'=> 'high',
'data' => [
'click_action'=> 'FLUTTER_NOTIFICATION_CLICK',
'status'=> 'done',
],
'to' => '/topics/all'
]);
或者你可以使用Firebase的云功能,对我来说,这是实现推送通知的更简单的方法。 重火力点/ functions-samples
这是使用CURL实现的
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_KEY_HERE",
'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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
$message是你要发送给设备的消息
$id是设备注册令牌
YOUR_KEY_HERE是你的服务器API密钥(或遗留的服务器API密钥)
使用Firebase Console可以根据应用程序包向所有用户发送消息。但对于CURL或PHP API,这是不可能的。
通过API可以向指定的设备ID或订阅的用户发送通知给选定的主题或订阅的主题用户。
Get a view on following link. It will help you.
https://firebase.google.com/docs/cloud-messaging/send-message
Firebase云消息传递有一个服务器端api,您可以调用它来发送消息。见https://firebase.google.com/docs/cloud-messaging/server。
发送消息可以像使用curl调用HTTP端点一样简单。看到https://firebase.google.com/docs/cloud-messaging/server implementing-http-connection-server-protocol
curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" \
--Header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
-d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}"
您可以在任何环境中使用所有这些REST API,但是这里列出的许多平台都有专门的所谓管理sdk。