我是 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:
messaging
快速启动应用 -- https://github.com/firebase/quickstart-android/tree/master/messaging - 已经设置了 Firebase 依赖项等.在 Android Studio
中加载克隆的应用程序.My Notification Client
的新项目,并使用 com.google.firebase.quickstart.fcm
作为 Android 的包名添加了一个应用应用程序.google-services.json
文件,并将其复制到 messaging/app/
文件夹并与 Grade 同步Android Studio
中的文件.Android Studio
中运行该应用程序.当应用程序在模拟器中加载后,单击 Log Token
按钮以在 Android Studio
的 Android Monitor
选项卡中捕获客户端的 Firebase 令牌.My Notification Client
,点击左侧窗格中的 Notifications
链接,并通过粘贴 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
.My Notification Client
and added an app using com.google.firebase.quickstart.fcm
as the package name of the Android app.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
.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
.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:
My Notification Server
的新项目.设置 >Firebase 控制台中的权限
,创建一个新的服务帐户并下载 serviceAccountCredentials.json
文件.My Notification Server
in Firebase Console.Settings > Permissions
in Firebase Console, created a new service account and downloaded the serviceAccountCredentials.json
file. <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>
然后,我扩展了 com.google.android.gcm.sender.Sender
以创建 FCMSender
并覆盖受保护的方法 getConnection(String url)
使用新的 FCM 端点,如下所示:
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 Key
Firebase 控制台中 My Notification Server
项目的 Cloud MessagingProject 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 云消息传递通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!