有没有办法通过FCM从一个Android设备向连接到Firebase数据库的另一个设备发送上行通知消息?
我知道XMPP服务器可以接收上行消息并将通知发送到其他设备。要接收使用上行API发送的消息,我需要实现XMPP服务器,但还有其他方法吗?
有没有办法通过FCM从一个Android设备向连接到Firebase数据库的另一个设备发送上行通知消息?
我知道XMPP服务器可以接收上行消息并将通知发送到其他设备。要接收使用上行API发送的消息,我需要实现XMPP服务器,但还有其他方法吗?
[DeviceA] -- please send message to B --> [SERVER] -- fcmSendAPI --> [DeviceB]
服务器可以简单到一个PHP页面,也可以是一个更复杂的XMPP实现。
在Node.js中的一个示例可以在这里找到:
使用Firebase数据库和云消息传递在设备之间发送通知
最终,在尝试自己维护可靠的服务器脚本 2 个月后,我突然发现了 OneSignal。它完全免费,在 iOS、Android、WP 和浏览器上支持设备到设备的推送消息。
希望不会因为宣传垃圾邮件而受到惩罚,但这目前是完全“无后端”的唯一(而且最简单)方式。
此外,这是完全安全的方法。除非知道特殊的 OS 用户 ID,否则没有人可以发送推送,您可以将其存储在 Firebase 数据库中并受到规则保护。
更新:它不是 Firebase 的替代品。它只有推送服务,没有其他功能。
更新2:Firebase 现在拥有 Functions,并且其使用示例已经发送 FCM。您现在不需要任何其他服务器或服务。在官方示例中阅读更多信息:https://github.com/firebase/functions-samples
经过多次尝试,最终我找到了一个解决方案,它完美地运作。
步骤1:包含两个库。
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);
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();
}
。
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(jsonArray,"Hello","How r u","Http:\\google.com","My Name is Vishal");
}
});