通过服务器端代码的Firebase通知

新的Firebase通知服务允许我们使用控制台用户界面向所有移动应用用户发送通知。

但是我找不到任何适用于Firebase通知服务的REST API。 我想通过基于事件的服务器端代码自动发送通知给移动用户。 Firebase通知服务是否具有可通过HTTP / S访问的API?

是的你可以。

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"}]} 

大多! 该文档位于Firebase云消息传递下: https ://firebase.google.com/docs/cloud-messaging/downstream

主要区别在于,通知控制台发送的消息会自动进行一些Firebase Analytics跟踪:对于您自己发送的消息,您可能需要添加一些手动跟踪事件。

@Narendra Naidu,嗨,你可以尝试这个代码片段的服务器端推送通知。 在你的服务器端项目代码中创build简单的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(); } 

请通过这个参考文件:

  1. https://firebase.google.com/docs/cloud-messaging/http-server-ref
  2. https://firebase.google.com/docs/cloud-messaging/server

它为您提供服务器端信息,以便从服务器发送通知到Firebase服务器到客户端应用程序。 此外,find下面的普通的Java代码文件(服务器端类),您将从中得到一些快速的想法。

请让我知道,如果我能得到进一步的帮助。