1.pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<!-- Firebase Admin SDK -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
</dependency>
<!--pubsub-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.14.0</version>
</dependency>
2.yml配置
spring:
cloud:
gcp:
credentials:
location: file:/data/reco-data/********.json
project-id: *****
pubsub:
enabled: true
google:
auth-file-path: /data/reco-data/*****.json
project-num-id: *****
androidApps: *****
firebase:
topic: ***
3.代码
package com.****.service.impl;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.gcp.pubsub.core.publisher.PubSubPublisherTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.****.service.DataRelayService;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class DataRelayServiceImp implements DataRelayService {
private final RestTemplate restTemplate;
public DataRelayServiceImp(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
@Autowired
private PubSubPublisherTemplate pubSubTemplate;
@Value("${google.project-num-id}")
private String projectNumId;
@Value("${google.firebase.topic}")
private String firebaseTopic;
@Value("${google.auth-file-path}")
private String filePath;
@Value("#{'${google.androidApps}'.split(',')}")
private List<String> androidApps;
@Override
public void getApiData() {
// 加载服务账号密钥
try {
List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/firebase.readonly",
"https://www.googleapis.com/auth/cloud-platform");
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(filePath)).createScoped(scopes);
// 强制刷新以获取新的访问令牌
credentials.refresh();
AccessToken token = credentials.getAccessToken();
// 获取令牌值
String accessToken = token.getTokenValue();
log.info("token----" + token);
for (String androidApp : androidApps) {
String url = new StringBuilder("https://fcmdata.googleapis.com/v1beta1/projects/")
.append(projectNumId)
.append("/androidApps/")
.append(androidApp)
.append("/deliveryData")
.toString();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + accessToken);
// 创建 HttpEntity
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
// 发送 GET 请求
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
log.info("==============" + response.getBody());
ByteString data = ByteString.copyFromUtf8(response.getBody());
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
String res = pubSubTemplate.publish(firebaseTopic, pubsubMessage).get();
log.info("pubsub 发布结果 = {}", res);
}
} catch (Exception e) {
log.error("firebase异常=========" + e);
}
}
}