我使用的是 Java 版本 8,Play 框架版本为 2.2.6。 我的 firbase-admin 依赖版本是 6.8.1.
我尝试使用提供的 json 文件在我的代码中初始化 firebase。分享firebase的初始化代码。
/**
*
*/
package util.notificationChannel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.FirebaseApp;
import com.google.auth.oauth2.GoogleCredentials;
@Service
public class FCMInitializer {
static Logger LOGGER = LoggerFactory.getLogger(FCMInitializer.class);
private static final String FIREBASE_CONFIGURATION_PATH = "./kronos-firebase-1182c-firebase-adminsdk.json";
public static void initialize() {
try (FileInputStream serviceAccount = new FileInputStream(FIREBASE_CONFIGURATION_PATH)) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
int bytesRead;
while ((bytesRead = serviceAccount.read(temp)) != -1) {
buffer.write(temp, 0, bytesRead);
}
byte[] data = buffer.toByteArray();
// Use a new ByteArrayInputStream for GoogleCredentials
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(inputStream)).build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
LOGGER.info("Firebase application has been initialized");
}
sendTestNotification();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
private static void sendTestNotification() {
try {
// Create a test message (for a specific device or topic)
Message message = Message.builder()
.putData("title", "Test")
.putData("body", "Firebase connection verified!")
.setTopic("test-topic") // Or use a device token
.build();
// Send the message
String response = FirebaseMessaging.getInstance().send(message);
System.out.println("Test notification sent successfully. Response: " + response);
} catch (Exception e) {
LOGGER.error("Failed to send test notification: " + e.getMessage(), e);
}
}
}
我已确保收到 json 凭据。我还收到如下成功消息
测试通知发送成功。回复:projects/kronos-204907/messages/8849451443820292620
但是与 firebase 连接的应用程序没有收到通知。 任何想法或建议都会非常有帮助。 谢谢你
我认为你需要创建
Notification
类并传递到 Message 中,就像这样 :
private static void sendTestNotification() {
try {
// Create a test message (for a specific device or topic)
Notification notification = Notification.builder()
.setTitle("YOUR_TITLE")
.setBody("YOUR_BODY/DESCRIPTION")
.setImage("YOUR_IMAGE") //This field is optional
.build();
Message message = Message.builder()
.setNotification(notification) //Notification data
.setTopic("test-topic") // Or use a device token
.build();
// Send the message
String response = FirebaseMessaging.getInstance().send(message);
System.out.println("Test notification sent successfully. Response: " + response);
} catch (Exception e) {
LOGGER.error("Failed to send test notification: " + e.getMessage(), e);
}
}
我只是编辑你的代码,在你的java代码中尝试这样
希望这对您有用。