Search code examples
androidangularspring-bootfirebase-cloud-messaginggoogle-cloud-messaging

How to push notification on Google Firebase FCM using spring boot?


I am looking for a solution to push notification from server (Spring Hibernate RESTful API) to client application, coded in Angular and Android. I want to implement a solution which does not eat too much bandwidth of the user as my userbase will have limited connectivity (low bandwidth and 3G/2G mobile network). This rules out polling as a possible solution. Next, I researched into socket connections but it looks like the socket connections can be left open and cause memory leak problems. Next, I am giving Google FCM a try but stuck in the implementation. Specifically, I am trying to send notification on FCM, but not receiving it on client side. To recap, I have to send a notification from JAVA layer (Spring boot), and receive it on angular as well as Android. I have tried some sample code to do this job using FCM. But it's not working. Please suggest me the solution or different ways to make this job at work. Here is the code I am working with:

    JSONObject body = new JSONObject();
            body.put("to", "/users/" + TOPIC);
            body.put("priority", "high");

            JSONObject notification = new JSONObject();
            notification.put("title", "JSA Notification");
            notification.put("body", "Happy Message!");

            JSONObject data = new JSONObject();
            data.put("Key-1", "JSA Data 1");
            data.put("Key-2", "JSA Data 2");

            body.put("notification", notification);
            body.put("data", data);

            HttpEntity<String> entity = new HttpEntity<>(body.toString());
            RestTemplate restTemplate = new RestTemplate();
            ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
            interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
            interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
            restTemplate.setInterceptors(interceptors);
            String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);
            CompletableFuture<String> pushNotification = CompletableFuture.completedFuture(firebaseResponse);
            CompletableFuture.allOf(pushNotification).join();
        try {
            String firebaseResponse = pushNotification.get();
            System.out.println("reponse "+firebaseResponse);
            return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

From the firebaseResponse, I get below response in my browser:

{"multicast_id":6197426474050349577,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

It looks like my message is not being sent. Any sample working code will be very much appreciated. Thanks for the help. - Pranav


Solution

  • FIREBASE_SERVER_KEY is wrong. If FIREBASE_SERVER_KEY is correct then the response should be below.

    {"multicast_id":7163645588662808791,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1563534037236589%eec6c0c0eec6c0c0"}]}