Android 如何使用带有云函数的http API使用fcm服务器发送fcm通知
我只需要发送包含2个firebase数据库的应用程序的通知。我正在从一个firebase数据库向一个应用程序发送通知。但应用程序没有收到通知Android 如何使用带有云函数的http API使用fcm服务器发送fcm通知,android,firebase-authentication,firebase-cloud-messaging,google-cloud-functions,Android,Firebase Authentication,Firebase Cloud Messaging,Google Cloud Functions,我只需要发送包含2个firebase数据库的应用程序的通知。我正在从一个firebase数据库向一个应用程序发送通知。但应用程序没有收到通知 i have the same problem but resolve using this code 实现这个库 实现'com.squareup.okhttp3:okhttp:3.4.1' static OkHttpClient mClient; static JSONArray jsonArray; static Co
i have the same problem but resolve using this code
实现这个库
实现'com.squareup.okhttp3:okhttp:3.4.1'
static OkHttpClient mClient;
static JSONArray jsonArray;
static Context context;
static String messageStr = "";
public static void sendNotification(Context mContext,String mMessage,final JSONArray jsonArray1) {
mClient = new OkHttpClient();
context = mContext;
messageStr = mMessage;
jsonArray = jsonArray1;
new MyTask().execute();
}
static class MyTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject root = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("text", messageStr);
notification.put("title", "Cell Command");
notification.put("line1", R.mipmap.ic_launcher);
notification.put("line2", "high");
root.put("to", jsonArray.get(i));
root.put("data", notification);
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) {
if (result != null) {
try {
JSONObject resultJson = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
static String postToFCM(String bodyString) throws IOException {
final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
final MediaType JSON
= MediaType.parse("application/json");
RequestBody body = RequestBody.create(JSON, bodyString);
Request request = new Request.Builder()
.url(FCM_MESSAGE_URL)
.post(body)
.addHeader("Authorization", "key=" + "put your firebase legacy key")
.build();
Response response = mClient.newCall(request).execute();
return response.body().string();
}
静态OkHttpClient mClient;
静态JSONArray JSONArray;
静态语境;
静态字符串messageStr=“”;
公共静态void sendNotification(上下文mContext、字符串mMessage、最终JSONArray jsonArray1){
mClient=new-OkHttpClient();
上下文=mContext;
messageStr=m消息;
jsonArray=jsonArray1;
新建MyTask().execute();
}
静态类MyTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
for(int i=0;i
在jsonarray中放置您的设备令牌
希望这对您有所帮助。发布您的代码或日志,并询问错误。这不是教程网站。