How to send upstream messages with FCM from androi

2020-06-05 05:48发布

We were using GoogleCloudMessaging.getInstance(context).send(context.getString(R.string.gcm_defaultSenderId) + "@gcm.googleapis.com", mId, mBundle); to send upstream messages, but since I was trying to migrate new fcm concept, I need to change that too, but could not find any documentation yet.

My best guess is to use :

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

but then what is it the Builder takes as a parameter? Yet again, could not find api...

So simply as title states, how to send upstream messages using new fcm concept?

2条回答
等我变得足够好
2楼-- · 2020-06-05 06:16

Well you can send your message directly to android devices from android application, here is the simple implementation I have done and it works great for me.

  1. compile android volley library

    compile 'com.android.volley:volley:1.0.0'
    
  2. Just copy paste this simple function ;) and your life will become smooth just like knife in butter. :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);
    }
    

Note If you want to send message to topics so you can change parameter instanceIdToken to something like /topics/topicName. For groups implementation is the same but you just need to take care of parameters. checkout Firebase documentation and you can pass those parameters. let me know if you face any issue.

查看更多
甜甜的少女心
3楼-- · 2020-06-05 06:26

Well, my answer comes fast. Keeping question and answer for future reference. I have found the answer on https://firebase.google.com/docs/cloud-messaging/android/upstream#sample-send

thanks to google's smart exclusion to search results 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.

new API would be like:

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());
查看更多
登录 后发表回答