通过服务器端代码发送的 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"}]} 

@Narendra 奈杜, 嗨,您可以尝试将此代码片段用于服务器端推送通知。 在服务器端项目代码中创建简单的 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代码文件(服务器结束类)中找到这个,您可以从中快速了解它。

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

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

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

最新更新