【问题标题】:How to send the fcm notifications using FCM server using http API with cloud functions如何使用具有云功能的 http API 使用 FCM 服务器发送 fcm 通知
【发布时间】:2019-01-17 14:40:18
【问题描述】:

我需要为包含 2 个 firebase 数据库的应用发送通知。我从一个 firebase 数据库向应用程序发送通知。但是应用没有收到通知

【问题讨论】:

  • 发布您的代码或 logcat 并询问错误。这不是教程网站。

标签: android firebase-authentication firebase-cloud-messaging google-cloud-functions


【解决方案1】:
    i have the same problem but resolve using this code

实现这个库 实现 'com.squareup.okhttp3:okhttp:3.4.1'

    static OkHttpClient mClient;
    static JSONArray jsonArray;
    static Context context;
    static String messageStr = "";



     public static void sendNotification(Context mContext,String mMessage,final JSONArray jsonArray1) {

            mClient = new OkHttpClient();
            context = mContext;
            messageStr = mMessage;

            jsonArray = jsonArray1;

            new MyTask().execute();

        }




 static class MyTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {


            try {

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject root = new JSONObject();
                    JSONObject notification = new JSONObject();
                    notification.put("text", messageStr);


                    notification.put("title", "Cell Command");
                    notification.put("line1", R.mipmap.ic_launcher);
                    notification.put("line2", "high");

                    root.put("to", jsonArray.get(i));
                    root.put("data", notification);


                    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) {

            if (result != null) {
                try {
                    JSONObject resultJson = new JSONObject(result);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "" + e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }


    static String postToFCM(String bodyString) throws IOException {

        final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";

        final MediaType JSON
                = MediaType.parse("application/json");

        RequestBody body = RequestBody.create(JSON, bodyString);
        Request request = new Request.Builder()
                .url(FCM_MESSAGE_URL)
                .post(body)
                .addHeader("Authorization", "key=" + "put your firebase legacy key")
                .build();
        Response response = mClient.newCall(request).execute();
        return response.body().string();
    }

在 jsonarray 里面放你的设备令牌 希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 2016-09-27
    • 2017-11-24
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2020-02-02
    相关资源
    最近更新 更多