我正在为我的大学节制作一个android应用程序,并希望在应用程序中附加FCM通知功能,这样,每次我都不必从POSTMAN发送通知,但它不起作用时,而是在{{3 }},效果很好。我想通过应用程序执行此操作。
public class Coordinators extends AppCompatActivity {
EditText Title, Text, Details;
Button send;
DatabaseReference databaseReference;
String title, text, details;
String url = "https://fcm.googleapis.com/fcm/send";
String token = "key=AIzaS...";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coordinators);
databaseReference = FirebaseDatabase.getInstance().getReference("Notification");
Title = (EditText) findViewById(R.id.title);
Text = (EditText) findViewById(R.id.text);
Details = (EditText) findViewById(R.id.details);
send = (Button) findViewById(R.id.Send_notification);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title = Title.getText().toString();
text = Text.getText().toString();
details = Text.getText().toString();
send_notification();
}
});
}
这是我的send_notification方法。
private void send_notification() {
JSONObject jsonObject1 = new JSONObject();
try {
jsonObject1.put("extra_information", details);
jsonObject1.put("title", title);
jsonObject1.put("text", text);
} catch (JSONException e) {
e.printStackTrace();
Log.e("CATCH Exception1: ", e.toString());
Toast.makeText(getApplicationContext(), "Some error Occurred :(", Toast.LENGTH_LONG).show();
}
JSONObject jsonObject2 = new JSONObject();
try {
jsonObject2.put("priority", "high");
} catch (JSONException e) {
e.printStackTrace();
Log.e("CATCH Exception2: ", e.toString());
Toast.makeText(getApplicationContext(), "Some error Occurred :(", Toast.LENGTH_LONG).show();
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("to", "/topics/NEWS");
jsonObject.put("collapse_key", "type_a");
jsonObject.put("data", jsonObject1.toString());
jsonObject.put("android", jsonObject2.toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e("CATCH Exception3: ", e.toString());
Toast.makeText(getApplicationContext(), "Some error Occurred :(", Toast.LENGTH_LONG).show();
}
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(Coordinators.this, "Successfully Sent", Toast.LENGTH_SHORT).show();
Log.e("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error: ", error.toString());
Toast.makeText(getApplicationContext(), "Please try again!", Toast.LENGTH_SHORT).show();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params2 = new HashMap<String, String>();
params2.put("Authorization", token);
Log.e("HEADER", params2.toString() + "\n\n\n");
return params2;
}
};
requestQueue.add(jsonObjectRequest).setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 30000;
}
@Override
public int getCurrentRetryCount() {
return 0;
}
@Override
public void retry(VolleyError error) throws VolleyError {
Toast.makeText(getApplicationContext(), "Slow Internet Connection", Toast.LENGTH_SHORT).show();
}
});
}
这是我从邮递员寄来的邮件
{
"to": "/topics/NEWS",
"collapse_key": "type_a",
"data": {
"extra_information": "This is a test notification.",
"title": "Test",
"text": "Test"
}
}
我在POSTMAN中应用了标头:
Content-Type application/json
Authorization key=AIzaS...
并使用POST方法https://fcm.googleapis.com/fcm/send
通过url发送它