3

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

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

4

3 回答 3

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"}]} 
于 2016-07-04T11:34:35.613 回答
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();
}

请仔细阅读此参考文档:

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

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

如果我能提供进一步的帮助,请告诉我。

于 2016-08-17T09:23:20.333 回答
1

大多!该文档位于 Firebase Cloud Messaging 下:https ://firebase.google.com/docs/cloud-messaging/downstream

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

于 2016-05-23T20:35:24.283 回答