如何在不使用 XMPP 或任何其他脚本的情况下使用 FCM 将设备发送到设备通知。?

How to send Device to device notification by using FCM without using XMPP or any other script.?

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

我知道 XMPP 服务器然后可以接收上游消息并将通知发送给另一个 devices.To 接收与上游一起发送的消息 API 我需要实现一个 XMPP 服务器但是没有其他方式???

经过多次尝试,我终于找到了一个解决方案,而且效果很好

第一步:包含两个库。

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 步:创建一个向所有设备发送通知的异步任务。

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 步:调用按钮的 onclick

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

Is there any way to send Upstream notification message through FCM from one android device to another devices connected with Firebase database?

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

完整详情:

  1. 向用户设备发送消息是一项非常严肃的操作!
    根据有效负载,消息可能导致垃圾邮件、网络钓鱼、执行内部方法。
  2. 您希望此操作只允许受信任的实体,这就是 FCM 发送 API 需要 SERVER-API-KEY 身份验证 header 的原因。
  3. 在您的应用程序代码中添加 SERVER-API-KEY(或以其他方式将其传达给应用程序)不安全。这是因为apk可以被提取、反编译、检查、在模拟器上执行、在调试下执行等等。

今天实现这个的最好方法: 是在两个设备之间有某种服务器:

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

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

可在此处找到 Node.js 中的示例:
Sending notifications between devices with Firebase Database and Cloud Messaging

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

希望,我不会收到促销垃圾邮件的标记,但这是目前唯一(也是最简单)的方式来完全 "backendless" .

另外,这是完全安全的方式。没有人可以发送推送,除非他知道特殊的 OS 用户 ID,您可以将其存储在受规则保护的 Firebase 数据库中。

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

UPD2:Firebase 现在有函数,它的用法示例有发送 FCM。您现在不需要任何其他服务器或服务。在官方 示例中阅读更多内容 https://github.com/firebase/functions-samples