Antonio Mele
Antonio Mele

Reputation: 387

Can't send firebase notification via Android but it works via curl

I am trying to send a push notification from one device to another using Firebase (they are two different projects). I am calling the API via okhttp3 library using POST. If I try it using curl the notification can be sent but via android I got the following error:

{"multicast_id":XXXXXXXX,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

Is there some metadata sent with the rest call in Android? How can I sent

this is the curl call (that works fine)

curl -H "Content-type: application/json" -H "Authorization:key=AXXXXX" -X POST -d '{"data":{"info":"abc"},"to":"DEV-TOKEN-XXX"}' https://fcm.googleapis.com/fcm/send

in Android I just replicate the same scenario setting header, body ecc with okhttp3

Upvotes: 0

Views: 551

Answers (2)

Antonio Mele
Antonio Mele

Reputation: 387

It worked when I used the "Legacy server key" within Android making a rest call. But with curl I use the "Server key" and it works fine. Don't know why.

Upvotes: 0

Mohit Dholakia
Mohit Dholakia

Reputation: 252

You can achieve this using below way:

1)First get the Token of the device in which you need to send Push Notification

FirebaseInstanceId.getInstance().getToken();

2)Then you need to create JSON object having two keys 'to' & 'notification'.You can also use 'data'.which has payload information stored.

JSONObject jsonObj = new JSONObject();
jsonObj.put("data", profileJson);
jsonObj.put("notification", notificationJson);
jsonObj.put("to", mPushToken);

3)Then make an API call which sends push notification.

RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonData.toString());
okhttp3.Request request = new okhttp3.Request.Builder()
        .header("Authorization", "key=" + R.string.firebase_legacy_server_key)
        .url("**https://fcm.googleapis.com/fcm/send**")
        .post(body)
        .build();

This will fires push notification to the given firebase token.

Upvotes: 0

Related Questions