5

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
guness
  • 6,336
  • 7
  • 59
  • 88

2 Answers2

10

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());
guness
  • 6,336
  • 7
  • 59
  • 88
1

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.

Muhammad Adil
  • 4,358
  • 3
  • 32
  • 36
  • so you want to expose `YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE` which should be kind of secret? – guness Apr 24 '17 at 09:58
  • Yes that is true it should be a secret. But point is that if someone can not do server side code then this solution can be used. I have seen people wants some kind of testing code for prototype of projects. – Muhammad Adil Apr 24 '17 at 10:57
  • 1
    Am getting Error "Unexpected response code 400 for https://fcm.googleapis.com/fcm/send" when I try up your snippet. What can I do? – Neri Sep 22 '17 at 16:48
  • 1
    @Neri i received the same error message as well, any solutions ? – ACAkgul May 10 '19 at 12:22
  • 1
    I have moved on , this implementation may have changed now. :) You shall apply server side script for a better solution . @ACAkgul – Muhammad Adil May 13 '19 at 10:13