Firebase Cloud Messaging notification from Java instead of Firebase Console

前端 未结 7 1753
野趣味
野趣味 2020-12-12 16:21

I am new to Firebase and I am running into errors such as mismatch sender id and Authentication Error with HTTP status 401 when my Java application tries to send a notificat

相关标签:
7条回答
  • 2020-12-12 17:11

    I wrote an FCMHelper Class, that works fine and is simple to use.

    See here https://github.com/MOSDEV82/fcmhelper

    0 讨论(0)
  • 2020-12-12 17:15

    I too have recently started using Firebase, with no prior experience in Google Cloud Messaging. AFAIK, there's no server side SDK for Firebase Cloud Messaging, currently.

    To send notifications/messages from your App Server to your Mobile Client, you need to implement your own HTTP and/or XMPP methods. See https://firebase.google.com/docs/cloud-messaging/

    My testing code, using HTTP:

    public CompletableFuture<String> fcmTest1() {
        // https://firebase.google.com/docs/cloud-messaging/http-server-ref
        String host = "fcm.googleapis.com";
        String requestURI = "/fcm/send";
        String CLIENT_TOKEN = "ASK_YOUR_MOBILE_CLIENT_DEV"; // https://developers.google.com/instance-id/
    
        CompletableFuture<String> fut = new CompletableFuture<>();
        JsonObject body = new JsonObject();
        // JsonArray registration_ids = new JsonArray();
        // body.put("registration_ids", registration_ids);
        body.put("to", CLIENT_TOKEN);
        body.put("priority", "high");
        // body.put("dry_run", true);
    
        JsonObject notification = new JsonObject();
        notification.put("body", "body string here");
        notification.put("title", "title string here");
        // notification.put("icon", "myicon");
    
        JsonObject data = new JsonObject();
        data.put("key1", "value1");
        data.put("key2", "value2");
    
        body.put("notification", notification);
        body.put("data", data);
    
        HttpClientRequest hcr = httpsClient.post(443, host, requestURI).handler(response -> {
            response.bodyHandler(buffer -> {
                logger.debug("FcmTest1 rcvd: {}, {}", response.statusCode(), buffer.toString());
                if (response.statusCode() == 200) {
                    fut.complete(buffer.toString());
                } else {
                    fut.completeExceptionally(new RuntimeException(buffer.toString()));
                }
            });
        });
        hcr.putHeader("Authorization", "key=" + Utils.FIREBASE_SERVER_KEY)
            .putHeader("content-type", "application/json").end(body.encode());
        return fut;
    }
    

    note: http client and json were provided by vertx (http://vertx.io/), but hopefully the code shows what's going on clear enough so you can use whatever you wish.

    0 讨论(0)
  • 2020-12-12 17:19

    I needed to send FCM notification from my java EE application and I found this very helpful. Might save someone some time.

    ...The github repo is a library interface for Firebase Cloud Messaging (FCM) API (NB// I am not the author). So I followed instructions in the readme: added the jar file as a dependency to my project and could just invoke the library methods with respective parameters.

    e.g. Pushraven.setKey(my_key); to set your server key and

    Notification raven = new Notification();
    raven.title("MyTitle")
         .text("Hello World!")
         .color("#ff0000")
         .to(client_key);
    

    to build a notification. The instructions from the repo are pretty much clear and helpful.

    0 讨论(0)
  • 2020-12-12 17:20

    On:

    // Obtain serverKey from Project Settings -> Cloud Messaging tab // for "My Notification Server" project in Firebase Console. String serverKey = <get_server_key_from_firebase_console>;

    You must obtain the key for your android app.

    And thank you, I was struggling to build a service to send/receive notifications for the app. Saw your question, tried to get it right, and used to build mine. Hope this will help you :)

    0 讨论(0)
  • 2020-12-12 17:20

    Use this sending push notifications from java.

    Spring Framework

    Compared to other projects there are just 3 simple classes with minimal dependencies.

    https://github.com/AndreiD/testingpushnotifications

    Remember to replace the SERVER_API_KEY and the Client_Token.

    0 讨论(0)
  • 2020-12-12 17:22

    I was running into the same issue. The problem was in the line:

    String serverKey = <get_server_key_from_firebase_console>;
    

    This value should come from the Android app project; in this case it would be "My Notification Client" and not "My Notification Server".

    0 讨论(0)
提交回复
热议问题