【问题标题】:Send request for firebase notification from the app从应用发送 Firebase 通知请求
【发布时间】:2017-05-09 10:38:13
【问题描述】:

我在我的 Android 应用中使用 Firebase 服务推送通知。该服务运行良好,我能够从 firebase 控制台管理员向所有用户和单个用户发送通知。

我的问题是:我想在用户在应用上发布帖子后发送自动通知(不使用管理员)。

  1. 用户发布帖子
  2. 使用用户 ID 向 Firebase API 发送请求
  3. Firebase 生成任务通知(30 分钟)并发送给用户

从哪里开始?有什么想法吗?

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    使用 okHttp3 库向 firebase 发送请求以向特定用户发送通知。

     RequestBody requestBody = null;
                try {
                    requestBody = RequestBody.create(MEDIA_TYPE_JSON, getValidJsonBody().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                Request request = new Request.Builder()
                        .addHeader(CONTENT_TYPE, APPLICATION_JSON)
                        .addHeader(AUTHORIZATION, AUTH_KEY)
                        .url(FCM_URL)
                        .post(requestBody)
                        .build();
    
                Call call = new OkHttpClient().newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onGetAllUsersFailure: " + e.getMessage());
                    }
    
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful())
                            Log.e(TAG, "onResponse: " + response.body().string());
                    }
                });
            }
    
            private JSONObject getValidJsonBody() throws JSONException {
                JSONObject jsonObjectBody = new JSONObject();
                jsonObjectBody.put(KEY_TO, mReceiverFirebaseToken);
    
                JSONObject jsonObjectData = new JSONObject();
                jsonObjectData.put(KEY_TITLE, mTitle);
                jsonObjectData.put(KEY_TEXT, mMessage);
                jsonObjectData.put(KEY_USERNAME, mUsername);
                jsonObjectData.put(KEY_UID, mUid);
                jsonObjectData.put(KEY_FCM_TOKEN, mFirebaseToken);
                jsonObjectBody.put(KEY_DATA, jsonObjectData);
    
                return jsonObjectBody;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 2016-09-29
      • 2013-08-17
      • 2018-06-01
      • 1970-01-01
      相关资源
      最近更新 更多