<bdo id='6j4OX'></bdo><ul id='6j4OX'></ul>

  • <legend id='6j4OX'><style id='6j4OX'><dir id='6j4OX'><q id='6j4OX'></q></dir></style></legend><tfoot id='6j4OX'></tfoot>

  • <small id='6j4OX'></small><noframes id='6j4OX'>

    <i id='6j4OX'><tr id='6j4OX'><dt id='6j4OX'><q id='6j4OX'><span id='6j4OX'><b id='6j4OX'><form id='6j4OX'><ins id='6j4OX'></ins><ul id='6j4OX'></ul><sub id='6j4OX'></sub></form><legend id='6j4OX'></legend><bdo id='6j4OX'><pre id='6j4OX'><center id='6j4OX'></center></pre></bdo></b><th id='6j4OX'></th></span></q></dt></tr></i><div id='6j4OX'><tfoot id='6j4OX'></tfoot><dl id='6j4OX'><fieldset id='6j4OX'></fieldset></dl></div>

      1. 来自 Java 而不是 Firebase 控制台的 Firebase 云消息传递通知

        时间:2024-05-10
        <tfoot id='vuYbM'></tfoot>
            • <bdo id='vuYbM'></bdo><ul id='vuYbM'></ul>

              <legend id='vuYbM'><style id='vuYbM'><dir id='vuYbM'><q id='vuYbM'></q></dir></style></legend>
                <tbody id='vuYbM'></tbody>
              <i id='vuYbM'><tr id='vuYbM'><dt id='vuYbM'><q id='vuYbM'><span id='vuYbM'><b id='vuYbM'><form id='vuYbM'><ins id='vuYbM'></ins><ul id='vuYbM'></ul><sub id='vuYbM'></sub></form><legend id='vuYbM'></legend><bdo id='vuYbM'><pre id='vuYbM'><center id='vuYbM'></center></pre></bdo></b><th id='vuYbM'></th></span></q></dt></tr></i><div id='vuYbM'><tfoot id='vuYbM'></tfoot><dl id='vuYbM'><fieldset id='vuYbM'></fieldset></dl></div>

              <small id='vuYbM'></small><noframes id='vuYbM'>

                • 本文介绍了来自 Java 而不是 Firebase 控制台的 Firebase 云消息传递通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Firebase 的新手,当我的 Java 应用程序尝试使用客户端的 Firebase 令牌发送通知/消息时,我遇到了错误,例如发件人 ID 不匹配和 HTTP 状态为 401 的身份验证错误.请注意,使用 Firebase 控制台,我可以将消息发送到客户端.

                  I am new to Firebase and I am running into errors such as mismatch sender id and Authentication Error with HTTP status 401 when my Java application tries to send a notification/message using the client's Firebase token. Note that using Firebase Console, I am able to send the message to the client.

                  对于客户端应用,我做了以下操作:

                  For the client app, I did the following:

                  1. 从这里克隆的 Firebase messaging 快速启动应用 -- https://github.com/firebase/quickstart-android/tree/master/messaging - 已经设置了 Firebase 依赖项等.在 Android Studio 中加载克隆的应用程序.
                  2. 使用 Firebase 控制台,创建了一个名为 My Notification Client 的新项目,并使用 com.google.firebase.quickstart.fcm 作为 Android 的包名添加了一个应用应用程序.
                  3. 为添加到新创建项目的 Android 应用下载 google-services.json 文件,并将其复制到 messaging/app/ 文件夹并与 Grade 同步Android Studio 中的文件.
                  4. 创建了一个模拟器并在 Android Studio 中运行该应用程序.当应用程序在模拟器中加载后,单击 Log Token 按钮以在 Android StudioAndroid Monitor 选项卡中捕获客户端的 Firebase 令牌.
                  5. 使用 Firebase 控制台,选择新创建的项目 My Notification Client,点击左侧窗格中的 Notifications 链接,并通过粘贴 Firebase 向设备发送通知上一步中捕获的令牌.
                  1. Cloned Firebase messaging quickstart app from here -- https://github.com/firebase/quickstart-android/tree/master/messaging -- which is already setup with Firebase dependencies and such. Loaded the cloned app in Android Studio.
                  2. Using Firebase Console, created a new project called My Notification Client and added an app using com.google.firebase.quickstart.fcm as the package name of the Android app.
                  3. Downloaded google-services.json file for the Android app added to the newly created project and copied it to the messaging/app/ folder and sync'd with Grade files from within Android Studio.
                  4. Created an emulator and ran the app from within Android Studio. When the app got loaded in the emulator, clicked the Log Token button to capture the client's Firebase token in the Android Monitor tab in Android Studio.
                  5. Using Firebase Console, selected the newly created project My Notification Client, clicked the Notifications link from the left pane, and sent a notification to the device by pasting the Firebase token captured in the previous step.

                  效果很好!下一步是使用单独的简单 Java 应用程序(最终将成为通知服务器)将通知发送到客户端,而不是使用 Firebase 控制台.

                  This worked great! The next step was to to use a separate simple Java application (which would eventually become a notification server) to send the notification to the client instead of using Firebase Console.

                  为此,我按照此处列出的步骤进行操作 - https://firebase.google.com/docs/server/setup#prerequisites.具体来说,这些是我执行的步骤:

                  To accomplish this, I followed the steps outlined here -- https://firebase.google.com/docs/server/setup#prerequisites. Specifically, these are the steps that I performed:

                  1. 在 Firebase 控制台中创建了一个名为 My Notification Server 的新项目.
                  2. 使用 设置 >Firebase 控制台中的权限,创建一个新的服务帐户并下载 serviceAccountCredentials.json 文件.
                  3. 在Eclipse中新建一个maven项目,在pom.xml中添加如下依赖:
                  1. Created a new project called My Notification Server in Firebase Console.
                  2. Using Settings > Permissions in Firebase Console, created a new service account and downloaded the serviceAccountCredentials.json file.
                  3. Created a new maven project in Eclipse and added the following dependencies in pom.xml:

                      <dependency>
                        <groupId>com.google.gcm</groupId>
                        <artifactId>gcm-server</artifactId>
                        <version>1.0.0</version>
                      </dependency>
                      <dependency>
                        <groupId>com.googlecode.json-simple</groupId>
                        <artifactId>json-simple</artifactId>
                        <version>1.1.1</version>
                      </dependency>
                      <dependency>
                        <groupId>com.google.firebase</groupId>
                        <artifactId>firebase-server-sdk</artifactId>
                        <version>3.0.0</version>
                      </dependency>
                  

                  1. 然后,我扩展了 com.google.android.gcm.sender.Sender 以创建 FCMSender 并覆盖受保护的方法 getConnection(String url) 使用新的 FCM 端点,如下所示:

                  1. Then, I extended com.google.android.gcm.sender.Sender to create FCMSender and override protected method getConnection(String url) to use the new FCM endpoint as shown below:

                  class FCMSender extends Sender {
                  
                      public FCMSender(String key) {
                          super(key);
                      }
                  
                      @Override
                      protected HttpURLConnection getConnection(String url) throws IOException {
                          String fcmUrl = "https://fcm.googleapis.com/fcm/send";
                          return (HttpURLConnection) new URL(fcmUrl).openConnection();
                      }
                  }
                  

                • 我的 Java 应用程序的 main() 方法如下所示:

                  public static void main(String[] args) {
                      // Obtain serverKey from Project Settings -> Cloud Messaging tab
                      // for "My Notification Client" project in Firebase Console.
                      String serverKey = <get_server_key_from_firebase_console>;
                      Thread t = new Thread() {
                          public void run(){ 
                              try {
                                  Sender sender = new FCMSender(serverKey);
                                  Message message = new Message.Builder()
                                                    .collapseKey("message")
                                                    .timeToLive(3)
                                                    .delayWhileIdle(true)
                                                    .addData("message", "Notification from Java application");
                                                    .build();  
                  
                                  // Use the same token(or registration id) that was earlier
                                  // used to send the message to the client directly from
                                  // Firebase Console's Notification tab.
                                  Result result = sender.send(message,
                              "APA91bFfIFjSCcSiJ111rbmkpnMkZY-Ej4RCpdBZFZN_mYgfHwFlx-M1UXS5FqDBcN8x1efrS2md8L9K_E9N21qB-PIHUqQwmF4p7Y3U-86nCGH7KNkZNjjz_P_qjcTR0TOrwXMh33vp",
                                      1);
                                  System.out.println("Result: " + result.toString());
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          };
                          t.start;
                          try {
                              t.join();
                          }
                          catch (InterruptedException iex) {
                              iex.printStackTrace();
                          }
                      }
                  

                • 当我运行 Java 应用程序时,我收到 MismatchSenderId 错误.我尝试使用 curl 进行故障排除,如下所示,但得到了相同的错误:

                  When I run the Java application, I get MismatchSenderId error. I tried troubleshooting using curl as shown below but got the same error:

                  $ skey=<obtained_from_project_settings_cloud_messaging_tab_in_firebase_console>
                  $ curl -X POST --header "Authorization: key=$skey" 
                    --Header "Content-Type: application/json" 
                    https://fcm.googleapis.com/fcm/send 
                    -d "{"to":"APA91bFfIFjSCcSiJ111rbmkpnMkZY-Ej4RCpdBZFZN_mYgfHwFlx-M1UXS5FqDBcN8x1efrS2md8L9K_E9N21qB-PIHUqQwmF4p7Y3U-86nCGH7KNkZNjjz_P_qjcTR0TOrwXMh33vp","data":{"message":"Yellow"}}"
                  
                  {"multicast_id":7391851081942579857,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
                  

                  而不是使用 Project Settings > 中列出的 Server KeyFirebase 控制台中 My Notification Server 项目的 Cloud Messaging 选项卡,我尝试使用 Project Number 但这导致 InvalidRequestException: HTTP Status Code: 401.

                  Instead of using the Server Key listed on Project Settings > Cloud Messaging tab for My Notification Server project in Firebase Console, I tried using the Project Number but that caused InvalidRequestException: HTTP Status Code: 401.

                  错误代码的 Firebase 文档说明了关于 MismatchSenderId:

                  Firebase documentation of the error codes says this about MismatchSenderId:

                  发件人不匹配 200 + error:MismatchSenderId
                  注册令牌与特定的发件人组相关联.当客户端应用注册 FCM 时,它必须指定允许哪些发件人发送消息.在向客户端应用程序发送消息时,您应该使用其中一个发件人 ID.如果您切换到其他发件人,现有的注册令牌将不起作用.

                  Mismatched Sender 200 + error:MismatchSenderId
                  A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration tokens won't work.

                  我不知道在 Firebase 控制台中的何处/如何配置 Android 应用(已添加到 My Notification Client 项目)以能够接收消息.

                  I could not figure out where/how in Firebase Console one can configure the Android app(which was aded to the My Notification Client project) to be able to receive messages.

                  对此的任何帮助,将不胜感激!

                  Any help on this, would be appreciated!

                  推荐答案

                  我遇到了同样的问题.问题出在一行:

                  I was running into the same issue. The problem was in the line:

                  String serverKey = <get_server_key_from_firebase_console>;
                  

                  此值应来自 Android 应用项目;在这种情况下,它将是我的通知客户端"而不是我的通知服务器".

                  This value should come from the Android app project; in this case it would be "My Notification Client" and not "My Notification Server".

                  这篇关于来自 Java 而不是 Firebase 控制台的 Firebase 云消息传递通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  1. <tfoot id='H0tfU'></tfoot>
                  2. <i id='H0tfU'><tr id='H0tfU'><dt id='H0tfU'><q id='H0tfU'><span id='H0tfU'><b id='H0tfU'><form id='H0tfU'><ins id='H0tfU'></ins><ul id='H0tfU'></ul><sub id='H0tfU'></sub></form><legend id='H0tfU'></legend><bdo id='H0tfU'><pre id='H0tfU'><center id='H0tfU'></center></pre></bdo></b><th id='H0tfU'></th></span></q></dt></tr></i><div id='H0tfU'><tfoot id='H0tfU'></tfoot><dl id='H0tfU'><fieldset id='H0tfU'></fieldset></dl></div>
                  3. <legend id='H0tfU'><style id='H0tfU'><dir id='H0tfU'><q id='H0tfU'></q></dir></style></legend>

                        <tbody id='H0tfU'></tbody>

                      <small id='H0tfU'></small><noframes id='H0tfU'>

                          <bdo id='H0tfU'></bdo><ul id='H0tfU'></ul>