Firebase Cloud Messaging Topic [duplicate] - java

This question already has an answer here:
How to track FCM push notifications send form server side or Rest Client? [duplicate]
(1 answer)
Closed 5 years ago.
Here is my code that I am attempting to set up a firebase call to send a message to a topic. I get a 200 response code back but nothing appears on the FCM console. Am i doing something wrong.
public static void pushFCMNotification() throws Exception{
String authKey = Constants.AUTH_KEY_FCM;
String FMCurl = Constants.API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
JSONObject info = new JSONObject();
try {
info.put("title", "New notification"); // Notification title
info.put("body", "A new notification has been added to the notice board"); // Notification body
json.put("notification", info);
json.put("to", "/topics/notif"); //replace userDeviceIdKey with the unique notification key for the group
} catch (JSONException e) {
e.printStackTrace();
}
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
Thanks for the help in advance.

Messages sent through the REST API don't appear in the console (regardless if it's sent to a token or to a topic).
Usually, if you use the REST API to send to a token, you could view it in the Diagnostics Page. However, messages sent to topics don't appear there as well. (see the Possible duplicate post I linked in the comments section)

Related

Subscribing to RESTful API and getting notifications

I'm having trouble receiving notification from API in JSON format. I've made a SpringBoot application that gets entities from the URL from the server (port:1026). However, the API has a subscription and notification system that I am supposed to utilize.
I'm having trouble realizing the implementation of getting the notification from API. When I subscribe to API a JSON entity is sent that I'm subscribing to I send an endpoint URL (localhost on port:1028) on which the notification is being sent. (entity and endpoint are in the same POST request to API to subscribe).
The issue is I don't know how to listen to that notification and show it on a webpage so when a call is made on API for value of that entity to change I see the notification on server log and see it in real time on my browser webpage.
This is the code that needs to be reworked. Here I just get a GET call from API to see what entities are created but when I make a PUT/POST to API via postman, manual refreshing is needed in order to see the change, and it's not utilizing the subscription system.
I think I need some kind of GET listener from server (localhost:1026) in order to parse the entity.
try {
URL url = new URL("http://localhost:1026/v2/entities");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connection is made
int responseCode = conn.getResponseCode();
// 200 OK
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
informationString = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
informationString.append(scanner.nextLine());
logger.info("Entity updated");
}
//Close the scanner
scanner.close();
logger.info(String.valueOf(informationString));
//return String.valueOf(informationString);
}
} catch (Exception e) {
e.printStackTrace();
}
return String.valueOf(informationString);
You can use setTimeout() on the webpage to periodically call your API then display the response immediately

Protocol message contained an invalid tag 0

My goal is to create a realtime service alert feed and send it over to a server that I made in Java with an HTTP post request. The first step I did was to create a copy of the example alert feed posted here and it seems I was successfully able to do that as I was able to print it out the message. https://developers.google.com/transit/gtfs-realtime/examples/alerts
The next step that I did is to create an HTTP connection and send the feed over with the POST request. This is what I have in my client code and example here is the feed name.
String url = "https://localhost:8080";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/x-protobuf");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
example.build().writeDelimitedTo(wr);
wr.flush();
wr.close();
My server code is simply this so far.
ServerSocket server = new ServerSocket(8080);
System.out.println("Listening for connection on port 8080 ....");
while (true) {
try (Socket socket = server.accept()) {
FeedMessage feed = FeedMessage.parseDelimitedFrom(socket.getInputStream());
Date today = new Date();
String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
}
}
The question here is that I get the Protocol message contained an invalid tag 0 on the server side. I would like some help on trying to resolve this issue. Maybe I am not parsing it correctly.
Update #2
I have tried to parse the HTTP headers to get to the payload like comments have said. But my code hangs and the output to print the headers on the terminal looks serialized.
DataInputStream isr = new DataInputStream(socket.getInputStream());
Reader reader = new InputStreamReader(isr);
BufferedReader reader2 = new BufferedReader(reader);
String line = reader2.readLine();
System.out.println("get lines");
while (!line.isEmpty()) {
System.out.println(line);
line = reader2.readLine();
}
You are dealing with raw sockets at the server, but the payload is encoded in an http request body. You are going to need to parse the http request through to the payload, and when you have just the body : send that to protobuf. Right now, you're sending the http headers to protobuf, which doesn't make sense. That could mean parsing through to \r\n\r\n, but it would help if you could make use of the content-length header, and even better if you can use a pre-built http library.

Server to Firebase HTTP POST results in response message 200

Using Java code (scroll down to view) I am sending a notification message to my Android using FCM, when providing the correct server key token I receive the response message seen below.
The following response message is received from FCM after
Response: 200
Success Message: '{"multicast_id":-1,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}'
Error Message: ''
Process finished with exit code 0
This means the server key token is correct and there's some sort of authorization established with FCM. When I use incorrect server key token(s) I get a different error message. However, the message above although labeled "Success Message*" still states that the success value =0 and the failure value =1, error:InvalidRegistration. If this is indeed an error, does the error imply the notification was not received by FCM, or not by the endpoint Android application?
Android App
The Android application is able to receive notifications from FCM using the console. Does this mean the Android app is set to receive the same notifications from the Java server I wrote or does the app need additional code to process these notifications that are not sent from the console?
(Just for information purspose, code is clean and runs without errors, not all files of the project are shown below, just the relevant main function and HTTP POST file).
Java server
public class Sample {
private static String SERVER_KEY = "AAAA-NCJais:APA91b----CENSORED-------";
public static void main(String[] args) {
com.pushraven.Pushraven.setKey(SERVER_KEY);
// create Notification object
Notification raven = new Notification();
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("Hello", "World!");
data.put("Rami", "Imar");
data.put("Test1", "Test2");
// build raven message using the builder pattern
raven.to("/topics/ALL")
.collapse_key("a_collapse_key")
.priority(1)
.delay_while_idle(true)
.time_to_live(100)
.restricted_package_name("com.example.******")
.dry_run(true)
.data(data)
.title("Testing")
.body("Hello World!");
// push the raven message
FcmResponse response = Pushraven.push(raven);
// alternatively set static notification first.
Pushraven.setNotification(raven);
response = Pushraven.push();
// prints response code and message
System.out.println(response);
}
}
--------------------------------- other file ------------------------------
public static FcmResponse push(Notification n) {
if(FIREBASE_SERVER_KEY == null){
System.err.println("No Server-Key has been defined.");
return null;
}
HttpsURLConnection con = null;
try{
String url = API_URL;
URL obj = new URL(url);
con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
// Set POST headers
con.setRequestProperty("Authorization", "key="+FIREBASE_SERVER_KEY);
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
// Send POST body
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
writer.write(n.toJSON());
writer.close();
wr.close();
wr.flush();
wr.close();
con.getResponseCode();
}
catch(Exception e){
e.printStackTrace();
}
return new FcmResponse(con);
}
InvalidRegistration error means that the token you're sending the message to is invalid:
Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.
Double check the value you're passing in your to parameter. In your code, I see that you're using news. If you were intending to send to a topic, you'll have to add the prefix /topics/. So it should be something like /topics/news/. See the Topic Messaging docs for more details.

google could message-"error":"MismatchSenderId"?

I am using gcm to push notifications to one or multiple devices, However I constantly get the error message: "mismatched sender ID".
Here is my code:
public static void post(String apiKey){
try{
// prepare JSON
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
jData.put("message", "{good luck}");
jGcmData.put("to","token ID");
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
// Read GCM response.
InputStream inputStream = conn.getInputStream();
String resp = IOUtils.toString(inputStream);
System.out.println(resp);
} catch (IOException e) {
System.out.println("Unable to send GCM message. "+e);
}
}
Also, when I used jGcmData.put("to","/topics/foo-bar");instead of jGcmData.put("to","token ID");, the notification can be sent successfully. However what I want is to push notification to selected devices.
For the mismatched sender ID:
Try uninstalling the app and run it again.This will clear any created keys of the App.
error:MismatchSenderId
A registration token is tied to a certain group of senders. When a client app registers for GCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration tokens won't work.
According to this SO answer, ""mismatchSenderId happens because the app within the same device have logged with different keys.""
For the Topic Subscription / Topic Sending
This may be related to this Subscribe to topics suddenly throws "java.io.IOException: InternalServerError", it says that "We identified an issue in our backed that affected a small percentage of the topic subscriptions during the last 24 hours. The issue has already been fixed, and the subscriptions should work correctly on all devices."
I hope this helps you.

push notification module sending null

I am trying to send notification to registered devices using gcm. But it send "null" instead of message. I have tried lot of solutions. If any basic thing is missing kindly share. I have attached the code spinet.
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
String msg = "new order came";
jData.put("message", msg);
// Where to send GCM message.
if (!(id == null)) {
jGcmData.put("to", id);
} else {
jGcmData.put("to", "/topics/global");
}
// What to send in GCM message.
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
The url you are using is incorrect. It should be https://gcm-http.googleapis.com/gcm/send. See Server changes.

Categories

Resources