我想从我的Android应用程序中向FCM主题发送消息。通过Firebase控制台发送消息运行良好。但是我希望一旦用户执行了特定的操作,就会向所有订阅了特定主题的其他用户发送一条消息。
在文档中可以找到以下代码:
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
我不知道Message
是哪个类的。显然不是RemoteMessage
。
谢谢
答案 0 :(得分:1)
您可以通过 Volley 和 FCM API
完成此操作这里是一个示例,用于将用户的通知发送到“ newOrder”主题,并具有标题和正文
RequestQueue mRequestQue = Volley.newRequestQueue(this);
JSONObject json = new JSONObject();
try {
json.put("to", "/topics/" + "newOrder");
JSONObject notificationObj = new JSONObject();
notificationObj.put("title", "new Order");
notificationObj.put("body", "New order from : " + phoneNum.replace("+", " "));
//replace notification with data when went send data
json.put("notification", notificationObj);
String URL = "https://fcm.googleapis.com/fcm/send";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL,
json,
response -> Log.d("MUR", "onResponse: "),
error -> Log.d("MUR", "onError: " + error.networkResponse)
) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key=yourKey");
return header;
}
};
mRequestQue.add(request);
} catch (JSONException e) {
e.printStackTrace();
}
在Firebase项目中用服务器密钥 替换您的密钥
答案 1 :(得分:0)
无法使用Firebase Cloud Message将消息直接从一台Android设备发送到另一台设备。您将始终需要服务器(或其他受信任的环境)来执行此操作。请参阅此文档部分,其中显示how messages are sent和我的答案。此处:How to send one to one message using Firebase Messaging。
您共享的代码示例正在使用Java的Admin SDK发送消息,该消息应在受信任的环境中运行。无法在您的Android应用中使用它。
答案 2 :(得分:0)
请参阅此link。。您可以通过此Google官方文档进行所需的操作。
但是我可以向您展示我通过AndroidFastNetworking库编写的示例:
# plot
plt.figure(figsize=(16, 8))
for j in range(0, len(centers)*2, 2):
x_features = features[:, j]
y_features = features[:, j+1]
x_centers = centers[:, j]
y_centers = centers[:, j+1]
# add the data for each label to the plot
for i, l in enumerate(labels):
plt.plot(x_features[i], y_features[i], marker=markers[int(j/2)], color=colors[int(j/2)], alpha=0.5)
# add the centers
for k in range(len(centers)):
plt.scatter(x_centers[k], y_centers[k], marker="X", color=colors[int(j/2)])
# create the rectangles for the legend
patches = [Patch(color=v, label=k) for k, v in cmap.items()]
# create centers marker for the legend
black_x = Line2D([], [], color='k', marker='X', linestyle='None', label='centers', markersize=10)
# add the legend
plt.legend(title='Labels', handles=patches + [black_x], bbox_to_anchor=(1.04, 0.5), loc='center left', borderaxespad=0, fontsize=15)
plt.show()
答案 3 :(得分:-1)
这是一个使用 OkHttp 4.x 和 Kotlin 的实现:
// create the payload
val payload = JSONObject()
.put("key", "value")
// create the request body (POST request)
val mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = JSONObject()
.put("to", "/topics/my_topic")
.put("data", payload)
.toString().toRequestBody(mediaType)
// create request
val request = Request.Builder()
.url("https://fcm.googleapis.com/fcm/send")
.post(requestBody)
.addHeader("Authorization", "key=${server_key_please_replace}")
.addHeader("Content-Type", "application/json")
.build()
// execute the call
val response = OkHttpClient().newCall(request).execute()
val responseBody = response.body?.charStream()?.readLines()
val httpCode = response.code
// error handling goes here, it's an error if the http response code is not 200
// or if the responseBody contains an error message like
// [{"multicast_id":2633602252647458018,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}]
安全注意事项
不建议在客户端使用服务器 API 密钥,因为该密钥可以被攻击者提取,然后用于代表密钥所有者(也就是您)发送消息。然而,我认为在某些情况下风险相当低:
总结:虽然不建议在客户端使用服务器 API 密钥,但根据用例,安全风险非常低。许多应用无法仅为该功能运行后端服务,因此我认为在某些情况下使用服务器密钥客户端是合理的。