我开始使用新的 Google 通知服务,Firebase Cloud Messaging
.
感谢这段代码https://github.com/firebase/quickstart-android/tree/master/messaging我能够从我的Firebase 用户控制台到我的安卓设备。
是否有任何 API 或方法可以在不使用 Firebase 控制台的情况下发送通知?我的意思是,例如,一个 PHP API 或类似的东西,直接从我自己的服务器创建通知。
这使用 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 控制台没有保存推送消息数据到fcm.googleapis.com/fcm/send?
向浏览器发送推送通知,我可以从哪里获取设备注册 ID?
这很好用,但是由于我收到的文字很长{"multicast_id":3694931298664346108,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MessageTooBig"}]}
.可以做些什么来解决这个问题?
@AlishaLamichhane 您的消息是否大于 4096 字节?如果不是,您可以对您的消息进行 base64 编码(文本可能有问题)。如果它大于 4096 字节……嗯,这就是 FCM 限制。
to
替换registration_ids
使用服务 API。
网址:https://fcm.googleapis.com/fcm/send
方法类型:POST
标题:
Content-Type: application/json
Authorization: key=your api key
车身/有效载荷:
{
"notification": {
"title": "Your Title",
"text": "Your Text",
"click_action": "OPEN_ACTIVITY_1"
},
"data": {
"<some_key>": "<some_value>"
},
"to": "<device_token>"
}
在您的应用程序中使用此功能,您可以在要调用的活动中添加以下代码:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Ankit,我可以发送到特定的设备 ID。但是,我无法发送给所有人。我要放什么"to" : "to_id(firebase refreshedToken)"
而不是设备ID?"all"
根本不工作。我正在使用 C#WebRequest
发送通知。 @AshikurRahman 你的建议也欢迎。自 3-4 天以来,我一直在努力寻找。
没关系。我找到了解决方案。 to : “/topics/all” 将向所有设备发送通知,或者如果您只想针对 IOS 将 all 替换为 ios,对于 android,请替换为 `android’。这些是默认主题集。我猜。
阅读这篇博文了解更多详情 – >developer.com/…
@Ankit,嗨,您能否指定如何获取目标设备的 ID?
@AnandRajstackoverflow.com/questions/37787373/…您可以获得令牌并保存在服务器上的存储(数据库)中。
@AnkitAdlakha 如果我们不想使用 firebase API 怎么办。我有什么选择?
我们如何发送频道 ID?有什么建议吗?非常感谢。
使用 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 的示例
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!"
}
}
您在哪里找到端点 urlfcm.googleapis.com/fcm/send,它在firebase doc中没有提到吗?
@JR我创建了一个聊天应用程序,当用户向接收者发送消息时,接收者应该收到通知消息。在这种情况下,我该如何使用您的答案?那么我应该给字段“to”的值是什么?
@Anad Raj 在我的回答中提到“向特定设备发送消息”
正如 Frank 所说,您可以使用 Firebase Cloud Messaging (FCM) HTTP API 从您自己的后端触发推送通知。但你将无法
- 将通知发送到 Firebase 用户标识符 (UID) 和
- 向用户群发送通知(像在用户控制台上一样定位属性和事件)。
含义:您必须自己存储 FCM/GCM 注册 ID(推送令牌)或使用 FCM 主题订阅用户。请记住,FCM 不是 Firebase 通知的 API,它是一个较低级别的 API,没有调度或开放率分析。 Firebase 通知建立在 FCM 之上。
介绍
我编译了上面的大部分答案,并根据FCM HTTP 连接文档在 2021 年策划一个适用于 FCM 的解决方案。感谢哈姆扎·马利克因为他在上面非常有见地的回答。
先决条件
首先,确保您已将您的项目与 Firebase 相关联,并且您已在应用程序上设置了所有依赖项。如果还没有,请先前往FCM 配置文档
如果这样做了,您还需要从 API 复制项目的服务器响应密钥。前往您的Firebase 控制台,单击您正在处理的项目,然后导航到;
Project Settings(Setting wheel on upper left corner) -> Cloud Messaging Tab -> Copy the Server key
配置你的 PHP 后端
我编译了 Hamzah 的答案Ankit Adlakha 的 API 调用结构和 FCM Docs 提出以下 PHP 函数:
function sendGCM() {
// FCM API Url
$url = 'https://fcm.googleapis.com/fcm/send';
// Put your Server Response Key here
$apiKey = "YOUR SERVER RESPONSE KEY HERE";
// Compile headers in one variable
$headers = array (
'Authorization:key=' . $apiKey,
'Content-Type:application/json'
);
// Add notification content to a variable for easy reference
$notifData = [
'title' => "Test Title",
'body' => "Test notification body",
'click_action' => "android.intent.action.MAIN"
];
// Create the api body
$apiBody = [
'notification' => $notifData,
'data' => $notifData,
"time_to_live" => "600" // Optional
'to' => '/topics/mytargettopic' // Replace 'mytargettopic' with your intended notification audience
];
// Initialize curl with the prepared headers and body
$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, json_encode($apiBody));
// Execute call and save result
$result = curl_exec ( $ch );
// Close curl after call
curl_close ( $ch );
return $result;
}
自定义您的通知推送
要通过令牌提交通知,请使用'to' => 'registration token'
期待什么
我在我的网站后端设置了该功能,并在 Postman 上对其进行了测试。如果您的配置成功,您应该会收到与以下类似的响应;
{"message":"{"message_id":3061657653031348530}"}
您在哪里托管服务器以发送通知?
@David Corral,检查我的答案是否相同。stackoverflow.com/a/38992689/2122328
编写了一个 spring 应用程序来发送 FCM 通知,以防你想看看它是如何工作的 ->github.com/aniket91/WebDynamo/blob/master/src/com/osfg/…
您可以使用改造来向设备发送消息。stackoverflow.com/questions/37435750/…
阅读这篇博文了解更多详情developer.com/…
@David我创建了一个聊天应用程序,当用户向接收者发送消息时,接收者应该收到通知消息。在这种情况下,我该如何使用您的答案?那么我应该给字段“to”的值是什么?
您可以查看以下链接。它有 Spring java 实现stackoverflow.com/a/51172021/3073945