如何在不使用 XMPP 或任何其他脚本的情况下使用 FCM 向设备发送设备通知.? [英] How to send Device to device notification by using FCM without using XMPP or any other script.?
问题描述
有什么方法可以通过 FCM 将上游通知消息从一台 android 设备发送到与 Firebase 数据库连接的另一台设备.
我知道 XMPP 服务器然后可以接收上游消息并将通知发送到其他设备.要接收使用上游 API 发送的消息,我需要实现一个 XMPP 服务器,但还有其他方法吗???
经过多次尝试,我终于找到了一个解决方案,而且效果很好
第 1 步:包括两个库.
<块引用>编译'com.squareup.okhttp3:okhttp:3.4.1'编译 'com.google.firebase:firebase-messaging:9.2.0'
第 2 步:在您的 MainActivity 或您要发送通知的位置.
OkHttpClient mClient = new OkHttpClient();String refreshedToken = "";//添加使用firebase登录的用户刷新令牌.JSONArray jsonArray = new JSONArray();jsonArray.put(refreshedToken);
第 3 步:创建一个向所有设备发送通知的异步任务.
public void sendMessage(最终的JSONArray接收者,最终的字符串标题,最终的字符串主体,最终的字符串图标,最终的字符串消息){new AsyncTask() {@覆盖受保护的字符串 doInBackground(String... params) {尝试 {JSONObject root = new JSONObject();JSONObject 通知 = new JSONObject();Notification.put("body", body);通知.放置(标题",标题);通知.放置(图标",图标);JSONObject 数据 = 新的 JSONObject();data.put("message", message);root.put("通知", 通知);root.put("数据", 数据);root.put("registration_ids", 收件人);字符串结果 = postToFCM(root.toString());Log.d("主活动", "结果:" + 结果);返回结果;} 捕捉(异常前){ex.printStackTrace();}返回空;}@覆盖protected void onPostExecute(String result) {尝试 {JSONObject resultJson = new JSONObject(result);int 成功,失败;成功 = resultJson.getInt("success");failure = resultJson.getInt("失败");Toast.makeText(MainActivity.this, "Message Success:" + success + "Message Failed:" + failure, Toast.LENGTH_LONG).show();} catch (JSONException e) {e.printStackTrace();Toast.makeText(MainActivity.this, "消息失败,发生未知错误.", Toast.LENGTH_LONG).show();}}}.执行();}String postToFCM(String bodyString) 抛出 IOException {public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";最终的 MediaType JSON= MediaType.parse("application/json; charset=utf-8");RequestBody body = RequestBody.create(JSON, bodyString);请求请求 = new Request.Builder().url(Url.FCM_MESSAGE_URL).post(正文).addHeader("授权", "key=" + "你的服务器密钥").建造();响应 response = mClient.newCall(request).execute();返回 response.body().string();}
第 4 步:点击您的按钮调用
btnSend.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {sendMessage(jsonArray,"Hello","How r u","Http:\google.com","My Name is Vishal");}});
Is there any way to send Upstream notification message through FCM from one android device to another devices connected with Firebase database.
I know that XMPP server can then receive the upstream messages and send the notifications to the other devices.To receive messages sent with the upstream API i need to implement an XMPP server but there is any other way???
After lots of try finally i got one solution and its work perfectly
Step 1 :Include two library.
compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.google.firebase:firebase-messaging:9.2.0'
Step 2 : In your MainActivity or from where you want to send notifications.
OkHttpClient mClient = new OkHttpClient();
String refreshedToken = "";//add your user refresh tokens who are logged in with firebase.
JSONArray jsonArray = new JSONArray();
jsonArray.put(refreshedToken);
Step 3: Create one async task which sends notifications to all devices.
public void sendMessage(final JSONArray recipients, final String title, final String body, final String icon, final String message) {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
try {
JSONObject root = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("body", body);
notification.put("title", title);
notification.put("icon", icon);
JSONObject data = new JSONObject();
data.put("message", message);
root.put("notification", notification);
root.put("data", data);
root.put("registration_ids", recipients);
String result = postToFCM(root.toString());
Log.d("Main Activity", "Result: " + result);
return result;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject resultJson = new JSONObject(result);
int success, failure;
success = resultJson.getInt("success");
failure = resultJson.getInt("failure");
Toast.makeText(MainActivity.this, "Message Success: " + success + "Message Failed: " + failure, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Message Failed, Unknown error occurred.", Toast.LENGTH_LONG).show();
}
}
}.execute();
}
String postToFCM(String bodyString) throws IOException {
public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, bodyString);
Request request = new Request.Builder()
.url(Url.FCM_MESSAGE_URL)
.post(body)
.addHeader("Authorization", "key=" + "your server key")
.build();
Response response = mClient.newCall(request).execute();
return response.body().string();
}
Step 4 : Call in onclick of your button
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(jsonArray,"Hello","How r u","Http:\google.com","My Name is Vishal");
}
});
这篇关于如何在不使用 XMPP 或任何其他脚本的情况下使用 FCM 向设备发送设备通知.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!