如何从Android客户端发送FCM的上游消息?

时间:2016-05-20 03:24:38

标签: android firebase-cloud-messaging

我们使用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概念发送上游消息?

2 个答案:

答案 0 :(得分:10)

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

找到了答案

感谢谷歌智能排除搜索结果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());

答案 1 :(得分:0)

那么你可以从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);
    }
    
  3. 注意 如果您要向主题发送消息,则可以将参数 instanceIdToken 更改为 / topics / topicName 。 对于,实现是相同的,但您只需要处理参数。 checkout Firebase documentation and you can pass those parameters. 如果您遇到任何问题,请与我们联系。