【问题标题】:Firebase Notifications through server-side code通过服务器端代码的 Firebase 通知
【发布时间】:2016-05-23 19:21:34
【问题描述】:

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

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

【问题讨论】:

标签: android ios firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

是的,你可以。

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

【讨论】:

  • 必须加一个“;”在 $title 的末尾,将 $arryToSend 的名称更改为 $arrayToSend,但代码运行良好。谢谢!
【解决方案2】:

@Narendra Naidu,您好,您可以尝试使用此代码 sn-p 进行服务器端推送通知。在您的服务器端项目代码中创建简单的 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 代码文件(服务器端类)下面找到这个,您可以从中获得一些快速的想法。

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

【讨论】:

  • 嗨 Sandeep_Devhare,你有任何 java 类来使用 Fcm 为多个设备发送通知。
  • @Priya,实际上 FCM 提供了两种将消息定位到多个设备的方法:主题消息传递 允许您向已选择加入特定主题的多个设备发送消息。 设备群组消息传递,允许您向属于您定义的群组的多个设备发送消息。请参考官方文档firebase.google.com/docs/cloud-messaging/android/send-multiple谢谢。
  • 您好,此代码不适用于具有 SSL 安全性的服务器。它给了我例外“没有与 fcm.googleapis.com 匹配的主题替代 DNS 名称”。关于这个例外的任何想法?
  • @GarjpreetSingh,使用 'ldaps' 连接 URL 并将用户目录中的 'secure' 属性设置为 'false' 以使用不会验证主机名和证书是否匹配的 SSL 连接。跨度>
  • @GarjpreetSingh,可能是 SSL 证书过期了,所以禁用源代码中的 SSL 检查。尝试一些代码来禁用 SSL 检查也尝试使用我在之前的评论中提到的上述方法。
【解决方案3】:

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

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

【讨论】:

  • 谢谢伊恩。会试试这个。
猜你喜欢
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多