Pri*_*ola 6 notifications push-notification android-studio spring-boot flutter
我有一个 springboot 应用程序,我在自己的家庭服务器上托管该应用程序。我也有 sql 数据库设置。
\n对于前端,我\xe2\x80\x99m 计划使用 android 进行初始测试阶段,然后将其转移到 flutter。
\n我想知道如何从 Spring Boot 向前端应用程序发送通知。我看过一些关于如何通过 fire base 发送它的文章,但我想知道是否有\xe2\x80\x99s 另一种方法可以在不使用外部服务的情况下实现相同的目的。
\n我已经在 3 台电脑上设置了运行 Ubuntu 的服务器,该服务器对我的应用程序进行负载平衡,并希望使用其中一台来发送推送通知。
\n请按照以下步骤操作
摇篮
implementation 'com.google.firebase:firebase-admin:8.1.0'
梅文
Run Code Online (Sandbox Code Playgroud)<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>8.1.0</version> </dependency>
../src/main/resources/firebase-service-account.json
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
@SpringBootApplication
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
@Bean
FirebaseMessaging firebaseMessaging() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource("firebase-service-account.json").getInputStream());
FirebaseOptions firebaseOptions = FirebaseOptions
.builder()
.setCredentials(googleCredentials)
.build();
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "YOUR APP NAME");
return FirebaseMessaging.getInstance(app);
}
}
Run Code Online (Sandbox Code Playgroud)
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import org.springframework.stereotype.Service;
@Service
public class FirebaseMessagingService {
private final FirebaseMessaging firebaseMessaging;
public FirebaseMessagingService(FirebaseMessaging firebaseMessaging) {
this.firebaseMessaging = firebaseMessaging;
}
public void sendNotification(String title, String body, String token) throws FirebaseMessagingException {
Notification notification = Notification
.builder()
.setTitle(title)
.setBody(body)
.build();
Message message = Message
.builder()
.setToken(token)
.setNotification(notification)
// .putAllData(note.getData())
.build();
firebaseMessaging.send(message);
// For Send to multiple devices use Multicast Message Builder
MulticastMessage message = MulticastMessage
.builder()
.addAllTokens(<List Of Tokens>)
.setNotification(notification)
// .putAllData(note.getData())
.build();
firebaseMessaging.send(message);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7931 次 |
| 最近记录: |