如何从 android 客户端使用 FCM 发送上游消息?

How to send upstream messages with FCM from android client?

我们使用 GoogleCloudMessaging.getInstance(context).send(context.getString(R.string.gcm_defaultSenderId) + "@gcm.googleapis.com", mId, mBundle); 发送上游消息,但由于我试图迁移新的 fcm 概念,我也需要更改它,但还找不到任何文档。

我最好的猜测是使用:

RemoteMessage message = new RemoteMessage.Builder(<?>).setMessageId(mId).setData ...
FirebaseMessaging.getInstance().send(message);

但是 Builder 将什么作为参数?再一次,找不到 api...

如标题所述,如何使用新的fcm概念发送上游消息?

嗯,我的回答很快。保留问题和答案以备将来参考。我在 https://firebase.google.com/docs/cloud-messaging/android/upstream#sample-send

上找到了答案

感谢 google 对搜索结果的智能排除 In order to show you the most relevant results, we have omitted some entries very similar to the 2 already displayed. If you like, you can repeat the search with the omitted results included.

新 API 会像:

FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com")
  .setMessageId(Integer.toString(msgId.incrementAndGet()))
  .addData("my_message", "Hello World")
  .addData("my_action","SAY_HELLO")
  .build());

好吧,您可以从 android 应用程序将消息直接发送到 android 设备,这是我完成的简单实现,对我来说效果很好。

  1. 编译android排球库

    compile 'com.android.volley:volley:1.0.0'
    
  2. 只需复制粘贴这个简单的功能;),您的生活就会变得顺畅如刀。 :D

    public static void sendPushToSingleInstance(final Context activity, final HashMap dataValue /*your data from the activity*/, final String instanceIdToken /*firebase instance token you will find in documentation that how to get this*/ ) {
    
    
    final String url = "https://fcm.googleapis.com/fcm/send";
    StringRequest myReq = new StringRequest(Request.Method.POST,url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
                }
            }) {
    
        @Override
        public byte[] getBody() throws com.android.volley.AuthFailureError {
            Map<String,String> rawParameters = new Hashtable<String, String>();
            rawParameters.put("data", new JSONObject(dataValue).toString());
            rawParameters.put("to", instanceIdToken);
            return new JSONObject(rawParameters).toString().getBytes();
        };
    
        public String getBodyContentType()
        {
            return "application/json; charset=utf-8";
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "key="+YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE);
            return headers;
        }
    
    };
    
    Volley.newRequestQueue(activity).add(myReq);
    }
    

备注 如果您想向主题发送消息,您可以将参数 instanceIdToken 更改为 /topics/topicName 之类的内容。 对于 groups 实现是相同的,但您只需要注意参数。 checkout Firebase documentation and you can pass those parameters. 如果您遇到任何问题,请告诉我。