从 springboot 发送推送通知

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

Nil*_*nta 2

请按照以下步骤操作

  1. 安装依赖项(Gradle/Maven)

摇篮

implementation 'com.google.firebase:firebase-admin:8.1.0'

梅文

  <dependency>
       <groupId>com.google.firebase</groupId>
       <artifactId>firebase-admin</artifactId>
       <version>8.1.0</version>
  </dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 添加 firebase-service-account.json 文件

../src/main/resources/firebase-service-account.json

  1. 打开 MainApplication java 类
  <dependency>
       <groupId>com.google.firebase</groupId>
       <artifactId>firebase-admin</artifactId>
       <version>8.1.0</version>
  </dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 创建服务
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)
  1. 控制器内服务的使用

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)

  • 当用户安装您的应用程序时,此令牌来自 Android 应用程序,firebase 为该设备生成令牌,您需要将其保存在数据库中。请检查(https://pub.dev/packages/firebase_messaging/example) (2认同)