Vis*_*a ツ 20 iphone android xmpp firebase firebase-cloud-messaging
有没有办法通过FCM从一台Android设备向与Firebase数据库连接的其他设备发送上游通知消息.
我知道XMPP服务器然后可以接收上游消息并将通知发送到其他设备.要接收与上游API一起发送的消息,我需要实现XMPP服务器,但还有其他方法吗?
Die*_*ini 14
有没有办法通过FCM从一台Android设备向与Firebase数据库连接的其他设备发送上游通知消息?
目前,无法直接从一台设备向另一台设备发送消息.
(或者至少在没有引入巨大安全漏洞的情况下不可能:下面有更多细节)
详细信息:
SERVER-API-KEY在身份验证标头中的原因.SERVER-API-KEY应用程序代码(或以其他方式将其传递给应用程序)并不安全.这是因为apk可以在仿真器上进行提取,反编译,检查,执行,在调试时执行等等.今天实现这一目标的最佳方法是在两个设备之间安装某种服务器:
[DeviceA] -- please send message to B --> [SERVER] -- fcmSendAPI --> [DeviceB]
Run Code Online (Sandbox Code Playgroud)
服务器可以像PHP页面一样简单,也可以是更复杂的XMPP实现.
可以在此处找到Node.js中的示例:
使用Firebase数据库和云消息传递在设备之间发送通知
经过多次尝试终于我得到了一个解决方案并且它的工作完美
步骤1:包括两个库。
Run Code Online (Sandbox Code Playgroud)compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.google.firebase:firebase-messaging:9.2.0'
第 2 步:在您的 MainActivity 中或您想要发送通知的位置。
OkHttpClient mClient = new OkHttpClient();
String refreshedToken = "";//add your user refresh tokens who are logged in with firebase.
JSONArray jsonArray = new JSONArray();
jsonArray.put(refreshedToken);
Run Code Online (Sandbox Code Playgroud)
第 3 步:创建一个向所有设备发送通知的异步任务。
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();
}
Run Code Online (Sandbox Code Playgroud)
第 4 步:调用按钮的 onclick
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(jsonArray,"Hello","How r u","Http:\\google.com","My Name is Vishal");
}
});
Run Code Online (Sandbox Code Playgroud)