5

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

4

2 回答 2

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());
于 2016-05-20T03:39:31.680 回答
1

好吧,您可以从 android 应用程序将您的消息直接发送到 android 设备,这是我所做的简单实现,它对我来说非常有用。

  1. 编译android volley库

    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 之类的内容。对于的实现是相同的,但您只需要处理参数。查看 Firebase 文档,您可以传递这些参数。 如果您遇到任何问题,请告诉我。

于 2017-04-24T05:22:33.723 回答