Firebase Cloud Messaging notification from Java instead of Firebase Console -


i new firebase , running errors such mismatch sender id , authentication error http status 401 when java application tries send notification/message using client's firebase token. note using firebase console, able send message client.

for client app, did following:

  1. cloned firebase messaging quickstart app here -- https://github.com/firebase/quickstart-android/tree/master/messaging -- setup firebase dependencies , such. loaded cloned app in android studio.
  2. using firebase console, created new project called my notification client , added app using com.google.firebase.quickstart.fcm package name of android app.
  3. downloaded google-services.json file android app added newly created project , copied messaging/app/ folder , sync'd grade files within android studio.
  4. created emulator , ran app within android studio. when app got loaded in emulator, clicked log token button capture client's firebase token in android monitor tab in android studio.
  5. using firebase console, selected newly created project my notification client, clicked notifications link left pane, , sent notification device pasting firebase token captured in previous step.

this worked great! next step to use separate simple java application(which become notification server) send notification client instead of using firebase console.

to accomplish this, followed steps outlined here -- https://firebase.google.com/docs/server/setup#prerequisites. specifically, these steps performed:

  1. created new project called my notification server in firebase console.
  2. using settings > permissions in firebase console, created new service account , downloaded serviceaccountcredentials.json file.
  3. created new maven project in eclipse , added following dependencies in pom.xml:
    <dependency>       <groupid>com.google.gcm</groupid>       <artifactid>gcm-server</artifactid>       <version>1.0.0</version>     </dependency>     <dependency>       <groupid>com.googlecode.json-simple</groupid>       <artifactid>json-simple</artifactid>       <version>1.1.1</version>     </dependency>     <dependency>       <groupid>com.google.firebase</groupid>       <artifactid>firebase-server-sdk</artifactid>       <version>3.0.0</version>     </dependency> 
  1. then, extended com.google.android.gcm.sender.sender create fcmsender , overrode protected method getconnection(string url) use new fcm endpoint shown below:

    class fcmsender extends sender {      public fcmsender(string key) {         super(key);     }      @override     protected httpurlconnection getconnection(string url) throws ioexception {         string fcmurl = "https://fcm.googleapis.com/fcm/send";         return (httpurlconnection) new url(fcmurl).openconnection();     } } 
  2. the main() method of java application looks this:

    public static void main(string[] args) {     // obtain serverkey project settings -> cloud messaging tab     // "my notification client" project in firebase console.     string serverkey = <get_server_key_from_firebase_console>;     thread t = new thread() {         public void run(){              try {                 sender sender = new fcmsender(serverkey);                 message message = new message.builder()                                   .collapsekey("message")                                   .timetolive(3)                                   .delaywhileidle(true)                                   .adddata("message", "notification java application");                                   .build();                    // use same token(or registration id) earlier                 // used send message client directly                 // firebase console's notification tab.                 result result = sender.send(message,             "apa91bffifjsccsij111rbmkpnmkzy-ej4rcpdbzfzn_mygfhwflx-m1uxs5fqdbcn8x1efrs2md8l9k_e9n21qb-pihuqqwmf4p7y3u-86ncgh7knkznjjz_p_qjctr0torwxmh33vp",                     1);                 system.out.println("result: " + result.tostring());             } catch (exception e) {                 e.printstacktrace();             }         };         t.start;         try {             t.join();         }         catch (interruptedexception iex) {             iex.printstacktrace();         }     } 

when run java application, mismatchsenderid error. tried troubleshooting using curl shown below got same error:

$ skey=<obtained_from_project_settings_cloud_messaging_tab_in_firebase_console> $ curl -x post --header "authorization: key=$skey" \   --header "content-type: application/json" \   https://fcm.googleapis.com/fcm/send \   -d "{\"to\":\"apa91bffifjsccsij111rbmkpnmkzy-ej4rcpdbzfzn_mygfhwflx-m1uxs5fqdbcn8x1efrs2md8l9k_e9n21qb-pihuqqwmf4p7y3u-86ncgh7knkznjjz_p_qjctr0torwxmh33vp\",\"data\":{\"message\":\"yellow\"}}"  {"multicast_id":7391851081942579857,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"mismatchsenderid"}]} 

instead of using server key listed on project settings > cloud messaging tab my notification server project in firebase console, tried using project number caused invalidrequestexception: http status code: 401.

firebase documentation of error codes says mismatchsenderid:

mismatched sender 200 + error:mismatchsenderid
registration token tied group of senders. when client app registers fcm, must specify senders allowed send messages. should use 1 of sender ids when sending messages client app. if switch different sender, existing registration tokens won't work.

i not figure out where/how in firebase console 1 can configure android app(which aded my notification client project) able receive messages.

any on appreciated!

i running same issue. problem in line:

string serverkey = <get_server_key_from_firebase_console>; 

this value should come android app project; in case "my notification client" , not "my notification server".


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -