Reputation: 77
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);
}
Upvotes: 2
Views: 4131
Reputation: 37808
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.
Upvotes: 3