title: FirebaseApp with name [DEFAULT] doesn't exist. (For Spring Boot)
body: <p>I followed this link to create a fire base application-<a href="https://blog.mestwin.net/send-push-notifications-from-spring-boot-server-side-application-using-fcm/" rel="noreferrer">https://blog.mestwin.net/send-push-notifications-from-spring-boot-server-side-application-using-fcm/</a>
  And I am getting this error-FirebaseApp with name [DEFAULT] doesn't exist. (For Spring Boot)
  I have added the JSON Key and and its path in application properties....Am not able to figure out where I Am going wrong...
  This is my FCMInitializer-</p>
  
  <pre><code>
  import com.google.auth.oauth2.GoogleCredentials;
  import com.google.firebase.FirebaseApp;
  import com.google.firebase.FirebaseOptions;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.stereotype.Service;
  
  import javax.annotation.PostConstruct;
  import java.io.IOException;
  
  
  
  @Service
  public class FCMInitializer {
  
  
  
      @Value("${app.firebase-configuration-file}")
      private String firebaseConfigPath;
  
      Logger logger = LoggerFactory.getLogger(FCMInitializer.class);
  
      @PostConstruct
      public void initialize() {
          try {
              FirebaseOptions options = new FirebaseOptions.Builder()
                      .setCredentials(GoogleCredentials.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream())).build();
              if (FirebaseApp.getApps().isEmpty()) {
                  FirebaseApp.initializeApp(options);
                  logger.info("Firebase application has been initialized");
              }
          } catch (IOException e) {
              logger.error(e.getMessage());
          }
      }
  
  }
  </code></pre>
  
  <p>This is my FCM Service-</p>
  
  <pre><code>
  import com.google.firebase.messaging.*;
  import lombok.extern.slf4j.Slf4j;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Service;
  
  import java.time.Duration;
  import java.util.Map;
  import java.util.concurrent.ExecutionException;
  
  //import org.slf4j.LoggerFactory;
  //import com.highpeak.av.pushnotification.dto.PushNotificationRequest;
  
  @Slf4j
  @Service
  public class FCMService {
  
      private Logger logger = LoggerFactory.getLogger(FCMService.class);
  
      public void sendMessage(Map&lt;String, String&gt; data, PushNotificationRequest request)
              throws InterruptedException, ExecutionException {
          Message message = getPreconfiguredMessageWithData(data, request);
          String response = sendAndGetResponse(message);
          log.info("Sent message with data. Topic: " + request.getTopic() + ", " + response);
      }
  
      private Message getPreconfiguredMessageWithData(Map&lt;String, String&gt; data, PushNotificationRequest request) {
          return getPreconfiguredMessageBuilder(request).putAllData(data).setTopic(request.getTopic())
                  .build();
      }
  
  
  
      public void sendMessageWithoutData(PushNotificationRequest request)
              throws InterruptedException, ExecutionException {
          Message message = getPreconfiguredMessageWithoutData(request);
          String response = sendAndGetResponse(message);
          log.info("Sent message without data. Topic: " + request.getTopic() + ", " + response);
      }
  
  
  
      private String sendAndGetResponse(Message message) throws InterruptedException, ExecutionException {
          return FirebaseMessaging.getInstance().sendAsync(message).get();
      }
  
      private AndroidConfig getAndroidConfig(String topic) {
          return AndroidConfig.builder()
                  .setTtl(Duration.ofMinutes(2).toMillis()).setCollapseKey(topic)
                  .setPriority(AndroidConfig.Priority.HIGH)
                  .setNotification(AndroidNotification.builder().setSound(NotificationParameter.SOUND.getValue())
                          .setColor(NotificationParameter.COLOR.getValue()).setTag(topic).build()).build();
      }
  
      private ApnsConfig getApnsConfig(String topic) {
          return ApnsConfig.builder()
                  .setAps(Aps.builder().setCategory(topic).setThreadId(topic).build()).build();
      }
  
      private Message getPreconfiguredMessageWithoutData(PushNotificationRequest request) {
          return getPreconfiguredMessageBuilder(request).setTopic(request.getTopic())
                  .build();
      }
  
  
  
      private Message.Builder getPreconfiguredMessageBuilder(PushNotificationRequest request) {
          AndroidConfig androidConfig = getAndroidConfig(request.getTopic());
          ApnsConfig apnsConfig = getApnsConfig(request.getTopic());
          return Message.builder()
                  .setApnsConfig(apnsConfig).setAndroidConfig(androidConfig).setNotification(
                          new Notification(request.getTitle(), request.getMessage()));
      }
  
  
      public void sendMessageToToken(PushNotificationRequest request)
              throws InterruptedException, ExecutionException {
          Message message = getPreconfiguredMessageToToken(request);
          String response = sendAndGetResponse(message);
          log.info("Sent message to token. Device token: " + request.getToken() + ", " + response);
      }
  
  
      private Message getPreconfiguredMessageToToken(PushNotificationRequest request) {
          return getPreconfiguredMessageBuilder(request).setToken(request.getToken())
                  .build();
      }
  
  
  
  }
  </code></pre>
  
  <p>This is my PushNotification Service-</p>
  
  <pre><code>package com.example.MyFirstPushNotificationService.PushNotificationsDemo;
  
  import com.google.api.client.util.Value;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.scheduling.annotation.Scheduled;
  import org.springframework.stereotype.Service;
  
  import java.time.LocalDateTime;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.concurrent.ExecutionException;
  
  @Service
  public class PushNotificationService {
  
      @Value("#{${app.notifications.defaults}}")
      private Map&lt;String, String&gt; defaults;
  
      private Logger logger = LoggerFactory.getLogger(PushNotificationService.class);
      private FCMService fcmService;
  
      public PushNotificationService(FCMService fcmService) {
          this.fcmService = fcmService;
      }
  
      @Scheduled(initialDelay = 60000, fixedDelay = 60000)
      public void sendSamplePushNotification() {
          try {
              fcmService.sendMessageWithoutData(getSamplePushNotificationRequest());
          } catch (InterruptedException | ExecutionException e) {
              logger.error(e.getMessage());
          }
      }
  
      public void sendPushNotification(PushNotificationRequest request) {
          try {
              fcmService.sendMessage(getSamplePayloadData(), request);
          } catch (InterruptedException | ExecutionException e) {
              logger.error(e.getMessage());
          }
      }
  
      public void sendPushNotificationWithoutData(PushNotificationRequest request) {
          try {
              fcmService.sendMessageWithoutData(request);
          } catch (InterruptedException | ExecutionException e) {
              logger.error(e.getMessage());
          }
      }
  
  
      public void sendPushNotificationToToken(PushNotificationRequest request) {
          try {
              fcmService.sendMessageToToken(request);
          } catch (InterruptedException | ExecutionException e) {
              logger.error(e.getMessage());
          }
      }
  
  
      private Map&lt;String, String&gt; getSamplePayloadData() {
          Map&lt;String, String&gt; pushData = new HashMap&lt;&gt;();
          pushData.put("messageId", defaults.get("payloadMessageId"));
          pushData.put("text", defaults.get("payloadData") + " " + LocalDateTime.now());
          return pushData;
      }
  
  
      private PushNotificationRequest getSamplePushNotificationRequest() {
          PushNotificationRequest request = new PushNotificationRequest(defaults.get("title"),
                  defaults.get("message"),
                  defaults.get("topic"));
          return request;
      }
  
  
  }
  
  </code></pre>
  
  <p>This is my PushNotificationController-</p>
  
  <pre><code>
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class PushNotificationController {
  
      private PushNotificationService pushNotificationService;
  
      public PushNotificationController(PushNotificationService pushNotificationService) {
          this.pushNotificationService = pushNotificationService;
      }
  
      @PostMapping("/notification/topic")
      public ResponseEntity sendNotification(@RequestBody PushNotificationRequest request) {
          pushNotificationService.sendPushNotificationWithoutData(request);
          return new ResponseEntity&lt;&gt;(new PushNotificationResponse(HttpStatus.OK.value(), "Notification has been sent."), HttpStatus.OK);
      }
  
      @PostMapping("/notification/token")
      public ResponseEntity sendTokenNotification(@RequestBody PushNotificationRequest request) {
          pushNotificationService.sendPushNotificationToToken(request);
          return new ResponseEntity&lt;&gt;(new PushNotificationResponse(HttpStatus.OK.value(), "Notification has been sent."), HttpStatus.OK);
      }
  
      @PostMapping("/notification/data")
      public ResponseEntity sendDataNotification(@RequestBody PushNotificationRequest request) {
          pushNotificationService.sendPushNotification(request);
          return new ResponseEntity&lt;&gt;(new PushNotificationResponse(HttpStatus.OK.value(), "Notification has been sent."), HttpStatus.OK);
      }
  
      @GetMapping("/notification")
      public ResponseEntity sendSampleNotification() {
          pushNotificationService.sendSamplePushNotification();
          return new ResponseEntity&lt;&gt;(new PushNotificationResponse(HttpStatus.OK.value(), "Notification has been sent."), HttpStatus.OK);
      }
  }
  
  </code></pre>
  
  <p>This is my Notification Parameter-</p>
  
  <pre><code>
  public enum NotificationParameter {
      SOUND("default"),
      COLOR("#FFFF00");
  
      private String value;
  
      NotificationParameter(String value) {
          this.value = value;
      }
  
      public String getValue() {
          return this.value;
      }
  }   
  </code></pre>
  
  <p>This is my PushNotificationRequest-</p>
  
  <pre><code>
  
  public class PushNotificationRequest {
  
      private String title;
      private String message;
      private String topic;
      private String token;
  
      public PushNotificationRequest() {
      }
  
      public PushNotificationRequest(String title, String messageBody, String topicName) {
          this.title = title;
          this.message = messageBody;
          this.topic = topicName;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public String getTopic() {
          return topic;
      }
  
      public void setTopic(String topic) {
          this.topic = topic;
      }
  
      public String getToken() {
          return token;
      }
  
      public void setToken(String token) {
          this.token = token;
      }
  }
  
  
  </code></pre>
  
  <p>This is my push notification Response-</p>
  
  <pre><code>package com.example.MyFirstPushNotificationService.PushNotificationsDemo;
  
  public class PushNotificationResponse {
  
      private int status;
      private String message;
  
      public PushNotificationResponse() {
      }
  
      public PushNotificationResponse(int status, String message) {
          this.status = status;
          this.message = message;
      }
  
      public int getStatus() {
          return status;
      }
  
      public void setStatus(int status) {
          this.status = status;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  }
  </code></pre>
  
  <p>And this is my application-properties:</p>
  
  <pre><code>app.firebase-configuration-file=google/push-notifications-example-firebase-adminsdk.json
  app.notifications.defaults={topic: 'common', title: 'Common topic - Hello', message: 'Sending test message \uD83D\uDE42', token: 'ss22t03wz208eg:APA2idkkow223FE_0v5yHxqCLTyxAQafj6nWaqi4QzwZTW004q1PUux63UsFN', payloadMessageId: '123', payloadData: 'Hello. This is payload content.'}
  </code></pre>
  
  <p>Please forgive me,as i am still a beginner in Fire base.....I am not getting where I am gong wrong....I think its in application properties.....Also I am getting the error at-FirebaseMessaging.getInstance().sendAsync(message).get();  of FCMService class.
  Thanks in Advance</p>