Firebase send http cloud messaging from android app

RJB

I am currently sending firebase cloud messages from Postman, using an http post request. Its all working fine. I am now trying to make a simple Android app to send the messages from, . However it doesn't work and I get either InvalidRegistration or MissingRegistration or com.android.volley.AuthFailureError

In postman, I do an http post request to https://fcm.googleapis.com/fcm/send

With 2 headers:

Key: 'Authorization'   Value: 'key=AAAAnfdQ2jM:AP....'

Key: 'Content-Type'     Value: application/json

Then in body, using raw:

{
"to": "/topics/anytopic",


"data": {
    "my_message": "Hi everyone!",
   }

}

Where and how do I put this information in my volley http post request?

RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Response: " + response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            return params;
        }
    };
    queue.add(sr);

I can't find any example of this.

Thanks so much.

RJB

You need to add a JSON body like this:

        JSONObject jsonBody = new JSONObject();
    jsonBody.put("to", REUVEN_TOKEN_BE_YOUNG);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("my_message", "Keep eating healthy!");

    jsonBody.putOpt("data", jsonObject);


    final String requestBody = jsonBody.toString();


    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Response: " + response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "That didn't work..." + error);
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Authorization",BE_YOUNG_APP_KEY);

            return params;
        }



        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                return null;
            }
        }

    };
    queue.add(sr);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Notification from Firebase cloud messaging are received but they are not shown in the cloud messaging reports

分類Dev

How to send push notifications FROM an android app with Firebase?

分類Dev

Firebase Cloud Messaging Statistics API

分類Dev

Firebase messaging on android and permissions

分類Dev

Android Firebase Messaging: How Update UI from onMessageReceived()

分類Dev

Firebase Cloud Messaging - Error calling firebase.messaging()

分類Dev

Can I disconnect a HTTP connection to Firebase Cloud Messaging (FCM) every time?

分類Dev

通知に Google App Engine と Firebase Cloud Messaging を使用する

分類Dev

Firebase Cloud Messaging: App Server でのトークン更新の処理

分類Dev

Google Cloud Messaging for Android with Quickblox

分類Dev

Android : Google Cloud Messaging Error

分類Dev

How to send a firebase message to topic from Android

分類Dev

Android:Firebase Cloud Messaging(FCM)トピックに登録

分類Dev

Firebase Cloud関数「admin.messaging(...)。sendは関数ではありません」

分類Dev

Firebase Cloud Messaging - Managing Registration Tokens

分類Dev

Firebase Cloud Messaging AppDelegateエラー

分類Dev

Firebase Cloud Messaging notification not received on iOS

分類Dev

Firebase Cloud Messaging 通知キー名

分類Dev

Can I send a silent push notification from a Firebase cloud function?

分類Dev

Send information from Server to android app

分類Dev

Firebase Cloud Messaging(FCM)-HTTP V1APIまたはレガシーHTTPAPI?

分類Dev

Firebase Cloud Messaging - Target to single device not visible in Firebase console

分類Dev

Firebase Cloud Messaging: Device Group sending support in Firebase Admin SDK

分類Dev

Send image data to android app from App Engine

分類Dev

How to send silent push to iOS via Google Cloud Messaging

分類Dev

Android Firebase Cloud Messaging(FCM):subscribeToTopicは自動再試行を行いますか?

分類Dev

Firebase Cloud Messaging(FCM)用にAndroidでJsonデータを解析する方法

分類Dev

Does Firebase Cloud Messaging support VOIP pushkit services?

分類Dev

Firebase Cloud Messagingは無料ですか?

Related 関連記事

  1. 1

    Notification from Firebase cloud messaging are received but they are not shown in the cloud messaging reports

  2. 2

    How to send push notifications FROM an android app with Firebase?

  3. 3

    Firebase Cloud Messaging Statistics API

  4. 4

    Firebase messaging on android and permissions

  5. 5

    Android Firebase Messaging: How Update UI from onMessageReceived()

  6. 6

    Firebase Cloud Messaging - Error calling firebase.messaging()

  7. 7

    Can I disconnect a HTTP connection to Firebase Cloud Messaging (FCM) every time?

  8. 8

    通知に Google App Engine と Firebase Cloud Messaging を使用する

  9. 9

    Firebase Cloud Messaging: App Server でのトークン更新の処理

  10. 10

    Google Cloud Messaging for Android with Quickblox

  11. 11

    Android : Google Cloud Messaging Error

  12. 12

    How to send a firebase message to topic from Android

  13. 13

    Android:Firebase Cloud Messaging(FCM)トピックに登録

  14. 14

    Firebase Cloud関数「admin.messaging(...)。sendは関数ではありません」

  15. 15

    Firebase Cloud Messaging - Managing Registration Tokens

  16. 16

    Firebase Cloud Messaging AppDelegateエラー

  17. 17

    Firebase Cloud Messaging notification not received on iOS

  18. 18

    Firebase Cloud Messaging 通知キー名

  19. 19

    Can I send a silent push notification from a Firebase cloud function?

  20. 20

    Send information from Server to android app

  21. 21

    Firebase Cloud Messaging(FCM)-HTTP V1APIまたはレガシーHTTPAPI?

  22. 22

    Firebase Cloud Messaging - Target to single device not visible in Firebase console

  23. 23

    Firebase Cloud Messaging: Device Group sending support in Firebase Admin SDK

  24. 24

    Send image data to android app from App Engine

  25. 25

    How to send silent push to iOS via Google Cloud Messaging

  26. 26

    Android Firebase Cloud Messaging(FCM):subscribeToTopicは自動再試行を行いますか?

  27. 27

    Firebase Cloud Messaging(FCM)用にAndroidでJsonデータを解析する方法

  28. 28

    Does Firebase Cloud Messaging support VOIP pushkit services?

  29. 29

    Firebase Cloud Messagingは無料ですか?

ホットタグ

アーカイブ