- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我是 Firebase 的新手,当我的 Java 应用程序尝试使用客户端的 Firebase token 发送通知/消息时,我遇到了错误,例如发件人 ID 不匹配和 HTTP 状态为 401 的身份验证错误。请注意,使用 Firebase 控制台,我可以将消息发送到客户端。
对于客户端应用,我做了以下操作:
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 token 。 My Notification Client
,点击左侧 Pane 中的 Notifications
链接,并通过粘贴 Firebase 向设备发送通知上一步中捕获的 token 。效果很好!下一步是使用单独的简单 Java 应用程序(最终将成为通知服务器)将通知发送到客户端,而不是使用 Firebase 控制台。
为此,我按照此处列出的步骤进行操作——https://firebase.google.com/docs/server/setup#prerequisites .具体来说,这些是我执行的步骤:
My Notification Server
的新项目。Settings > Permissions
,创建一个新的服务帐户并下载 serviceAccountCredentials.json
文件。 <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
并覆盖 protected 方法 getConnection(String url)
使用新的 FCM 端点,如下所示:
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
进行故障排除,如下所示,但得到了相同的错误:
$ 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"}]}
我尝试使用 Firebase 控制台中 My Notification Server
项目的 Project Settings > Cloud Messaging
选项卡上列出的 Server Key
Project Number
但导致 InvalidRequestException: HTTP Status Code: 401
.
错误代码的 Firebase 文档说明了关于 MismatchSenderId
:
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
项目)以能够接收消息。
对此的任何帮助,将不胜感激!
最佳答案
我最近也开始使用 Firebase,之前没有 Google Cloud Messaging 方面的经验。 AFAIK,目前没有用于 Firebase 云消息传递的服务器端 SDK。
要将通知/消息从您的应用服务器发送到您的移动客户端,您需要实现自己的 HTTP 和/或 XMPP 方法。见 https://firebase.google.com/docs/cloud-messaging/
我的测试代码,使用 HTTP:
public CompletableFuture<String> fcmTest1() {
// https://firebase.google.com/docs/cloud-messaging/http-server-ref
String host = "fcm.googleapis.com";
String requestURI = "/fcm/send";
String CLIENT_TOKEN = "ASK_YOUR_MOBILE_CLIENT_DEV"; // https://developers.google.com/instance-id/
CompletableFuture<String> fut = new CompletableFuture<>();
JsonObject body = new JsonObject();
// JsonArray registration_ids = new JsonArray();
// body.put("registration_ids", registration_ids);
body.put("to", CLIENT_TOKEN);
body.put("priority", "high");
// body.put("dry_run", true);
JsonObject notification = new JsonObject();
notification.put("body", "body string here");
notification.put("title", "title string here");
// notification.put("icon", "myicon");
JsonObject data = new JsonObject();
data.put("key1", "value1");
data.put("key2", "value2");
body.put("notification", notification);
body.put("data", data);
HttpClientRequest hcr = httpsClient.post(443, host, requestURI).handler(response -> {
response.bodyHandler(buffer -> {
logger.debug("FcmTest1 rcvd: {}, {}", response.statusCode(), buffer.toString());
if (response.statusCode() == 200) {
fut.complete(buffer.toString());
} else {
fut.completeExceptionally(new RuntimeException(buffer.toString()));
}
});
});
hcr.putHeader("Authorization", "key=" + Utils.FIREBASE_SERVER_KEY)
.putHeader("content-type", "application/json").end(body.encode());
return fut;
}
注意:http 客户端和 json 由 vertx ( http://vertx.io/ ) 提供,但希望代码能够清楚地显示正在发生的事情,以便您可以随意使用。
关于java - 来自 Java 而不是 Firebase 控制台的 Firebase 云消息传递通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426542/
Github:https://github.com/jjvang/PassIntentDemo 我一直在关注有关按 Intent 传递对象的教程:https://www.javacodegeeks.c
我有一个 View ,其中包含自动生成的 text 类型的 input 框。当我单击“通过电子邮件发送结果”按钮时,代码会将您带到 CalculatedResults Controller 中的 Em
我有一个基本的docker镜像,我将以此为基础构建自己的镜像。我没有基础镜像的Dockerfile。 基本上,基本镜像使用两个--env arg,一个接受其许可证,一个选择在容器中激活哪个框架。我可以
假设我想计算 2^n 的总和,n 范围从 0 到 100。我可以编写以下内容: seq { 0 .. 100 } |> Seq.sumBy ((**) 2I) 但是,这与 (*) 或其他运算符/函数不
我有这个网址: http://www.example.com/get_url.php?ID=100&Link=http://www.test.com/page.php?l=1&m=7 当我打印 $_G
我想将 window.URL.createObjectURL(file) 创建的地址传递给 dancer.js 但我得到 GET blob:http%3A//localhost/b847c5cd-aa
我想知道如何将 typedef 传递给函数。例如: typedef int box[3][3]; box empty, *board[3][3]; 我如何将 board 传递给函数?我
我正在将一些代码从我的 Controller 移动到核心数据应用程序中的模型。 我编写了一个方法,该方法为我定期发出的特定获取请求返回 NSManagedObjectID。 + (NSManagedO
为什么我不能将类型化数组传递到采用 any[] 的函数/构造函数中? typedArray = new MyType[ ... ]; items = new ko.observableArray(ty
我是一名新的 Web 开发人员,正在学习 html5 和 javascript。 我有一个带有“选项卡”的网页,可以使网页的某些部分消失并重新出现。 链接如下: HOME 和 JavaScript 函
我试图将对函数的引用作为参数传递 很难解释 我会写一些伪代码示例 (calling function) function(hello()); function(pass) { if this =
我在尝试调用我正在创建的 C# 项目中的函数时遇到以下错误: System.Runtime.InteropServices.COMException: Operation is not allowed
使用 ksh。尝试重用当前脚本而不修改它,基本上可以归结为如下内容: `expr 5 $1 $2` 如何将乘法命令 (*) 作为参数 $1 传递? 我首先尝试使用“*”,甚至是\*,但没有用。我尝试
我一直在研究“Play for Java”这本书,这本书非常棒。我对 Java 还是很陌生,但我一直在关注这些示例,我有点卡在第 3 章上了。可以在此处找到代码:Play for Java on Gi
我知道 Javascript 中的对象是通过引用复制/传递的。但是函数呢? 当我跳到一些令人困惑的地方时,我正在尝试这段代码。这是代码片段: x = function() { console.log(
我希望能够像这样传递参数: fn(a>=b) or fn(a!=b) 我在 DjangoORM 和 SQLAlchemy 中看到了这种行为,但我不知道如何实现它。 最佳答案 ORM 使用 specia
在我的 Angular 项目中,我最近将 rxjs 升级到版本 6。现在,来自 npm 的模块(在 node_modules 文件夹内)由于一些破坏性更改而失败(旧的进口不再有效)。我为我的代码调整了
这个问题在这里已经有了答案: The issue of * in Command line argument (6 个答案) 关闭 3 年前。 我正在编写一个关于反向波兰表示法的 C 程序,它通过命
$(document).ready(function() { function GetDeals() { alert($(this).attr("id")); } $('.filter
下面是一个例子: 复制代码 代码如下: use strict; #这里是两个数组 my @i =('1','2','3'); my @j =('a','b','c'); &n
我是一名优秀的程序员,十分优秀!