如何通过使用FCM而不使用XMPP或任何其他脚本将设备发送到设备通知?

有没有办法通过FCM从一个Android设备发送上游通知消息到另一个与Firebase数据库连接的设备。

我知道XMPP服务器可以接收上游消息并将通知发送给其他设备。要接收使用上游API发送的消息,我需要实现XMPP服务器,但是还有其他方法吗?

有没有办法通过FCM从一个Android设备发送上游通知消息到另一个与Firebase数据库连接的设备?

目前无法直接从一台设备发送消息到另一台设备。
(或者至less在不引入巨大的安全漏洞的情况下是不可能的:下面的更多细节)

全部细节:

  1. 发送消息到用户设备是一个非常严重的行为!
    基于有效负载的消息可能会导致垃圾邮件,networking钓鱼,执行内部方法。
  2. 您希望此操作仅被允许为可信实体,这就是为什么FCM发送API需要身份validation标头中的SERVER-API-KEY
  3. 在您的应用程序代码中添加SERVER-API-KEY (或以其他方式将其传递给应用程序) 不安全 。 这是因为apk可以被提取,反编译,检查,在仿真器上执行,在debugging时执行等等。

今天实现这个最好的方法是在两个设备之间安装一些服务器:

 [DeviceA] -- please send message to B --> [SERVER] -- fcmSendAPI --> [DeviceB] 

服务器可以像PHP页面一样简单,也可以是更复杂的XMPP实现。

Node.js中的一个例子可以在这里find:
通过Firebase数据库和云消息传送在设备之间发送通知

最后,我自己试着维护可靠的服务器脚本2个月后,突然发现了OneSignal 。 它是完全免费的,支持iOS,Android,WP和浏览器上的设备到设备推送消息。

希望我不会得到升级垃圾邮件的标志, 但是它是目前唯一(最容易)完全“后端”的方式

而且,这是完全安全的方式。 没有人可以发送推送,除非他知道特殊的操作系统用户标识,您可以将其存储在受规则保护的Firebase数据库中。

UPD:这不是Firebase的替代品。 它只推送服务,没有别的

UPD2:Firebase现在具有函数,并且它的使用示例已经发送FCM。 您现在不需要任何其他服务器或服务。 官方样本https://github.com/firebase/functions-samples 阅读更多

经过大量的尝试,最终我得到了一个解决scheme,其工作完美

第1步:包含两个库。

 compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.google.firebase:firebase-messaging:9.2.0' 

第2步:在您的MainActivity或您想要发送通知的地方。

 OkHttpClient mClient = new OkHttpClient(); String refreshedToken = "";//add your user refresh tokens who are logged in with firebase. JSONArray jsonArray = new JSONArray(); jsonArray.put(refreshedToken); 

步骤3:创build一个向所有设备发送通知的asynchronous任务。

 public void sendMessage(final JSONArray recipients, final String title, final String body, final String icon, final String message) { new AsyncTask<String, String, String>() { @Override protected String doInBackground(String... params) { try { JSONObject root = new JSONObject(); JSONObject notification = new JSONObject(); notification.put("body", body); notification.put("title", title); notification.put("icon", icon); JSONObject data = new JSONObject(); data.put("message", message); root.put("notification", notification); root.put("data", data); root.put("registration_ids", recipients); String result = postToFCM(root.toString()); Log.d("Main Activity", "Result: " + result); return result; } catch (Exception ex) { ex.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { try { JSONObject resultJson = new JSONObject(result); int success, failure; success = resultJson.getInt("success"); failure = resultJson.getInt("failure"); Toast.makeText(MainActivity.this, "Message Success: " + success + "Message Failed: " + failure, Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "Message Failed, Unknown error occurred.", Toast.LENGTH_LONG).show(); } } }.execute(); } String postToFCM(String bodyString) throws IOException { public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send"; final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, bodyString); Request request = new Request.Builder() .url(Url.FCM_MESSAGE_URL) .post(body) .addHeader("Authorization", "key=" + "your server key") .build(); Response response = mClient.newCall(request).execute(); return response.body().string(); } 

第4步:通过点击button

  btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendMessage(jsonArray,"Hello","How ru","Http:\\google.com","My Name is Vishal"); } });