如何通过使用FCM而不使用XMPP或任何其他脚本将设备发送到设备通知? [英] How to send Device to device notification by using FCM without using XMPP or any other script.?
问题描述
我知道XMPP服务器可以接收上游消息并将通知发送给其他设备。要接收使用上游API发送的消息,我需要实现一个XMPP服务器,但是还有其他方法吗?
:包含两个库。
compile'com.squareup.okhttp3:okhttp: 3.4.1'
compile'com.google.firebase:firebase-messaging:9.2.0'
第二步:在您的MainActivity或您想要发送通知的地方。
OkHttpClient mClient = new OkHttpClient();
String refreshedToken =; //添加使用firebase登录的用户刷新标记。
JSONArray jsonArray = new JSONArray();
jsonArray.put(refreshedToken);
第3步:创建一个向所有设备发送通知的异步任务。
public void sendMessage(final JSONArray recipients,final String title,final String body,final String icon,final String message) b
$ b新的AsyncTask< String,String,String>(){
@Override
保护字符串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(通知,通知);
root.put(data,data);
root.put(registration_ids,收件人);
String result = postToFCM(root.toString());
Log.d(主要活动,结果:+结果);
返回结果;
catch(Exception ex){
ex.printStackTrace();
}
返回null;
$ b @Override
保护无效onPostExecute(字符串结果){
尝试{
JSONObject resultJson = new JSONObject(result);
int成功,失败;
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();
字符串postToFCM(String bodyString)抛出IOException {
public static final String FCM_MESSAGE_URL =https:// fcm .googleapis.com / FCM /发送;
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 =+你的服务器密钥)
.build();
响应响应= mClient.newCall(request).execute();
return response.body()。string();
第四步:通过点击按钮
btnSend.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
sendMessage(jsonArray,Hello,How ru,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");
}
});
这篇关于如何通过使用FCM而不使用XMPP或任何其他脚本将设备发送到设备通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!