我从新的谷歌通知服务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或类似的东西,直接从我自己的服务器创建通知。
当前回答
或者你可以使用Firebase的云功能,对我来说,这是实现推送通知的更简单的方法。 重火力点/ functions-samples
其他回答
这个链接的解决方案对我帮助很大。你可以去看看。
带有这些指令行的curl.php文件可以工作。
<?php
// Server key from Firebase Console define( 'API_ACCESS_KEY', 'AAAA----FE6F' );
$data = array("to" => "cNf2---6Vs9", "notification" => array( "title" => "Shareurcodes.com", "body" => "A Code Sharing Blog!","icon" => "icon.png", "click_action" => "http://shareurcodes.com"));
$data_string = json_encode($data);
echo "The Json Data : ".$data_string;
$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_POSTFIELDS, $data_string);
$result = curl_exec($ch);
curl_close ($ch);
echo "<p> </p>";
echo "The Result : ".$result;
请记住,您需要使用另一个浏览器执行curl.php文件,而不是从用于获取用户令牌的浏览器执行。只有在浏览其他网站时才能看到通知。
简介
我编译了上面的大部分答案,并根据FCM HTTP连接文档更新了变量,以制定一个在2021年适用于FCM的解决方案。感谢Hamzah Malik给出的非常有见地的回答。
先决条件
首先,确保你已经将你的项目与Firebase连接起来,并且你已经在你的应用程序上设置了所有的依赖项。如果你还没有,首先去看FCM配置文档
如果这样做了,您还需要从API复制项目的服务器响应键。转到Firebase控制台,单击正在进行的项目,然后导航到;
Project Settings(Setting wheel on upper left corner) -> Cloud Messaging Tab -> Copy the Server key
配置PHP后端
我用Ankit Adlakha的API调用结构和FCM文档编译了Hamzah的答案,得出了下面的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 ' => '注册令牌'
会发生什么
我在我的网站后台设置了这个功能,并在Postman上进行了测试。如果您的配置是成功的,您应该期望一个类似于下面的响应;
{"message":"{"message_id":3061657653031348530}"}
Go to cloud Messaging select: Server key
function sendGCM($message, $deviceToken) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"title" => "Notification title",
"body" => $message,
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_SERVER_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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ($ch);
}
例如,您可以使用用于谷歌云消息传递(GCM)的PHP脚本。Firebase及其控制台位于GCM之上。
我在github上找到了这个: https://gist.github.com/prime31/5675017
提示:这个PHP脚本会产生一个android通知。
因此:如果你想在Android中接收和显示通知,请阅读来自Koot的回答。
正如Frank所提到的,您可以使用Firebase Cloud Messaging (FCM) HTTP API从您自己的后端触发推送通知。但是你不能这样做
发送通知到Firebase用户标识符(UID)和 向用户群发送通知(就像你在用户控制台上所做的那样,瞄准属性和事件)。
意思是:您必须自己存储FCM/GCM注册id(推送令牌)或使用FCM主题订阅用户。还要记住,FCM不是Firebase通知的API,它是一个没有调度或打开率分析的低级API。Firebase Notifications是建立在FCM之上的。