新的Firebase通知服务允许我们使用控制台用户界面向所有移动应用用户发布通知。
但我找不到Firebase Notifications Service的任何REST API。我想根据事件通过服务器端代码自动向移动用户发送通知。 Firebase Notifications Service是否具有可通过HTTP / S访问的API?
答案 0 :(得分:10)
是的,你可以。
1)首先,获取firebase项目的服务器密钥:
Project Settings -> Cloud Messaging Tab -> Copy the Server key.
2)现在,这是一个示例php脚本,用于向特定设备发送通知:
<?php
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = "device_token_here";
//Title of the Notification.
$title = "The North Remembers";
//Body of the Notification.
$body = "Bear island knows no king but the king in the north, whose name is stark.";
//Creating the notification array.
$notification = array('title' =>$title , 'body' => $body);
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification);
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key= your_server_key_here';
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
curl_exec($ch);
//Close request
curl_close($ch);
?>
3)执行时输出:
{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]}
答案 1 :(得分:2)
晴!该文档位于Firebase云消息传递中:https://firebase.google.com/docs/cloud-messaging/downstream
主要区别在于,通知控制台发送的邮件会自动进行Firebase Analytics跟踪:对于您自己发送的邮件,您可能需要手动添加一些事件进行跟踪。
答案 2 :(得分:2)
@Narendra Naidu,您好,您可以尝试使用此代码段进行服务器端推送通知。在服务器端项目代码中创建简单的java类,并使用参数添加此方法,您还需要一些firebase凭据来执行此操作。请尝试以下。
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "ApidhfkIjd_cAdhpa-ZZ065hskiH53Hw3g";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
// userDeviceIdKey is the device id you will query from your database
public static void pushFCMNotification(String userDeviceIdKey) throws Exception{
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", "Notificatoin Title"); // Notification title
info.put("body", "Hello Test notification"); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
请仔细阅读以下参考文档:
它为您提供服务器端信息,用于从服务器向 - firebase服务器 - 客户端应用程序发送通知。 另外,在下面的普通java代码文件(服务器端类)中查找,您将从中获得一些快速的想法。
如果能得到进一步帮助,请告诉我。