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

Vis*_*a ツ 20 iphone android xmpp firebase firebase-cloud-messaging

有没有办法通过FCM从一台Android设备向与Firebase数据库连接的其他设备发送上游通知消息.

我知道XMPP服务器然后可以接收上游消息并将通知发送到其他设备.要接收与上游API一起发送的消息,我需要实现XMPP服务器,但还有其他方法吗?

Die*_*ini 14

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

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

详细信息:

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

今天实现这一目标的最佳方法是在两个设备之间安装某种服务器:

[DeviceA] -- please send message to B -->  [SERVER] -- fcmSendAPI --> [DeviceB]
Run Code Online (Sandbox Code Playgroud)

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

可以在此处找到Node.js中的示例:
使用Firebase数据库和云消息传递在设备之间发送通知


Vis*_*a ツ 2

经过多次尝试终于我得到了一个解决方案并且它的工作完美

步骤1:包括两个库。

compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.google.firebase:firebase-messaging:9.2.0'
Run Code Online (Sandbox Code Playgroud)

第 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);
Run Code Online (Sandbox Code Playgroud)

第 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();
    }
Run Code Online (Sandbox Code Playgroud)

第 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");
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • *此处为 firebaser* 这会将您的 FCM 服务器密钥放入 APK 中,恶意用户可以在其中找到它并使用它代表您发送消息。这是一个非常糟糕的主意,我强烈建议不要这样做。正如一位 FCM 工程师在[此答案](http://stackoverflow.com/a/37634914/209103)中所说:发送设备到设备消息需要您目前在服务器上运行可信进程。 (15认同)
  • 我一直在尝试:)看了很多教程,但我不知道如何将代码组合在一起。 (2认同)