過去是用GCM 但firebase出來就有FCM
一直想要試試看FCM APP打訊息給APP,終於成功了!
之前自己架伺服器設webserverAPI,再讓Android去呼叫伺服器API
這次用Android內直接打fcm send,但這樣做法是不建議,因為金鑰暴露在外面...
後來想只是自己的小APP 玩玩而已就練練隨便寫
=============================================================================
compile 'com.squareup.okhttp3:okhttp:3.3.0'
=============================================================================

public static final MediaType JSON
 = MediaType.parse("application/json; charset=utf-8");
=============================================================================
new AsyncTask<Void, Void, Void>() {

 @Override
 protected Void doInBackground(Void... params) {
 try {
 OkHttpClient client = new OkHttpClient();
 JSONObject json = new JSONObject();
 JSONObject dataJson = new JSONObject();
 dataJson.put("body", "Hi this is sent from device to device");
 dataJson.put("title", "dummy title");
 json.put("notification", dataJson);
 json.put("to", "暫時寫自己的TOKEN測試用");
 RequestBody body = RequestBody.create(JSON, json.toString());
 Request request = new Request.Builder()
 .header("Authorization", "key=AAAA0qHCG1g:APA91bEkkT.....JteHVBNbvc0h31xs3hAnq" )
 .url("https://fcm.googleapis.com/fcm/send")
 .post(body)
 .build();
 Response response = client.newCall(request).execute();
 String finalResponse = response.body().string();
 } catch (Exception e) {
 //Log.d(TAG,e+"");
 }
 return null;
 }
 @Override
 protected void onPostExecute(
 Void result) {
 }
}.execute();

=============================================================================

注意:KEY是在

參考資料:

How to send device to device messages using Firebase Cloud Messaging?

文章標籤
全站熱搜
創作者介紹
創作者 程式小試身手 的頭像
程式小試身手

程式小試身手

程式小試身手 發表在 痞客邦 留言(0) 人氣(264)