How to Send Device to Device Notification by Using Fcm Without Using Xmpp or Any Other Script.

How to send Device to device notification by using FCM without using XMPP or any other script.?

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 sending notification to specific device from server without client code modification

You will need to implement the FirebaseMessagingService in order to receive the notification. You can avoid this by sending a Notification tag instead of the data tag that you are currently using.

Note: if it's a notification tag push, it will be handled by system only if the application is not in foreground. If the app is in foreground, you will have to again handle it manually using your own code in the above mentioned service



Related Topics



Leave a reply



Submit