This project demonstrates how to integrate Spring Boot with Firebase Cloud Messaging (FCM) for sending push notifications, leveraging the latest features of JDK 17 or JDK 21 for optimal performance. It uses virtual threads available in JDK 21 to improve scalability and concurrency, and communicates directly with FCM using pure REST API calls. This approach reduces the application’s dependency footprint by avoiding the com.google.firebase:firebase-admin
dependency.
-
Direct FCM Integration via REST API: Interfaces with FCM through REST API calls, circumventing the need for the
com.google.firebase:firebase-admin
SDK. -
Virtual Thread Support: Takes advantage of JDK 21’s virtual threads for enhanced concurrency and scalability, particularly beneficial for I/O-bound operations. The application remains fully functional under JDK 17, albeit without the benefits of virtual threads.
-
Spring Boot Configuration: Focuses on a minimalist configuration approach within Spring Boot to maintain configurability and flexibility.
-
Robust Error Handling: Ensures reliable delivery of notifications through comprehensive error handling for REST interactions with FCM.
-
Efficient JWT Token Management: Implements efficient JWT token management for FCM authentication, optimized for REST API usage.
Before starting, ensure you have:
-
JDK 17 or JDK 21 (for virtual thread optimization)
-
Gradle Wrapper 8.6 (included in the project)
-
An FCM-configured Firebase project with REST API access
Follow these steps to get the project running locally:
-
Clone the repository:
git clone https://github.com/waileong/spring-boot-fcm-push-notification.git
-
Navigate to the project directory:
cd spring-boot-fcm-push-notification
-
Configure FCM for REST API access with your API keys and tokens.
-
Build the project using the Gradle Wrapper to ensure compatibility with Gradle 8.6:
./gradlew build
Add the following to your build.gradle
:
dependencies { implementation 'io.github.waileong:spring-boot-fcm:1.0.4' }
To send notifications through FCM using the REST API, utilize the FcmService
:
@Autowired
private FcmService fcmService;
public void sendNotification(FcmSendRequest request) {
// Define notification details here
fcmService.send(request);
}
Make sure your build.gradle
or pom.xml
excludes the Firebase Admin SDK, focusing instead on libraries essential for REST API communication.
Employ Spring’s RestClient
for FCM interactions, following modern HTTP communication best practices.
For users on JDK 21 looking to enable virtual threads:
spring.threads.virtual.enabled=true
This enhances the application’s performance and resource management by leveraging JDK 21’s concurrency improvements.