java - 使用 Firebase 云和 Java 向单个设备发送消息

标签 java firebase firebase-cloud-messaging

我正在尝试使用 Java 应用程序中的 token 将消息发送到单个设备。我正在使用 Firebase 管理 SDK。以下是我所拥有的

    FileInputStream serviceAccount = null;
    try {
        serviceAccount = new FileInputStream("google-services.json");
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }

    FirebaseOptions options = null;
    try {
        options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://MYPROJECTID.firebaseio.com/")
            .build();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    FirebaseApp.initializeApp(options);

    String registrationToken = "MYDEVICETOKEN";

    // See documentation on defining a message payload.
    Message message = Message.builder().putData("time", "2:45").setToken(registrationToken)
            .build();

    // Send a message to the device corresponding to the provided
    // registration token.
    String response = null;
    try {
        response = FirebaseMessaging.getInstance().sendAsync(message).get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    // Response is a message ID string.
    System.out.println("Successfully sent message: " + response);

但是我得到以下异常

java.io.IOException: Error reading credentials from stream, 'type' field not specified.

我在这里做错了什么?

最佳答案

单击“生成新私钥”按钮。 console snapshot

关于java - 使用 Firebase 云和 Java 向单个设备发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49533855/

相关文章:

flutter - Android 依赖项的编译和运行时版本不同 : runing FCM dependency

flutter - 当应用程序通过使用 firebase_messaging 的推送通知终止时如何接收自定义数据?

java - Hibernate 多对多标准投影

android - Firebase RecyclerView 未加载新数据

php - 使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 Android 应用程序中的特定设备

android - React Native + Android:transformClassesWithDexForDebug失败

firebase - Google Cloud Functions 出现 ECONNRESET 错误,直到我重新部署

java - 通用 TweenEngine 一次多个值

java - 代码没有错误,但模拟器表示应用已停止

java - RxJava 组合可观察量