如何使用firebase消息发送一对一消息

41zrol4v  于 2021-06-30  发布在  Java
关注(0)|答案(5)|浏览(370)

我一直在尝试阅读有关如何从一个设备发送消息到另一个的官方文档和指南。我在实时数据库中保存了两个设备的注册令牌,因此我有另一个设备的注册令牌。我试过以下方法来传递信息

  1. RemoteMessage message = new RemoteMessage.Builder(getRegistrationToken())
  2. .setMessageId(incrementIdAndGet())
  3. .addData("message", "Hello")
  4. .build();
  5. FirebaseMessaging.getInstance().send(message);

然而,这是行不通的。另一个设备没有收到任何消息。我甚至不确定是否可以使用上行消息发送来进行设备到设备的通信。
ps:我只想知道使用fcm是否可以进行设备到设备的消息传递?如果是,那么我使用的代码是否有问题?如果是,那么正确的方法是什么。
更新:
我的问题是,不使用firebase can以外的任何单独服务器的设备到设备的消息传递是否可行,如果可行,那么如何实现,因为没有相关文档。我不明白这里还有什么要解释的?无论如何,我得到了答案,并将更新它作为一个答案,一旦问题重新开放。

guykilcj

guykilcj1#

firebase有两个功能可以向设备发送消息:
firebase控制台中的通知面板允许您向用户订阅的特定设备、用户组或主题发送通知。
通过调用firebase云消息传递api,您可以使用您喜欢的任何目标策略发送消息。调用fcm api需要访问您的服务器密钥,您不应该在客户端设备上公开该密钥。这就是为什么你应该总是在应用服务器上运行这样的代码。
firebase文档直观地显示了这一点:

不支持通过firebase云消息传递将消息从一个设备直接发送到另一个设备。
更新:我写了一篇博客文章,详细介绍了如何使用firebase数据库、云消息和node.js在android设备之间发送通知。
更新2:您现在还可以使用firebase的云功能安全地发送消息,而无需启动服务器。请参阅此示例用例以开始。

esbemjvw

esbemjvw2#

我迟到了,但是上面的解决方案帮助我写下了这个简单的答案,你可以直接把你的信息从android应用程序发送到android设备,这是我做的简单的实现,它对我来说非常有用。
编译android截击库

  1. compile 'com.android.volley:volley:1.0.0'

只需复制粘贴这个简单函数;)你的生活就会变得一帆风顺,就像黄油刀一样d

  1. public static void sendPushToSingleInstance(final Context activity, final HashMap dataValue /*your data from the activity*/, final String instanceIdToken /*firebase instance token you will find in documentation that how to get this*/ ) {
  2. final String url = "https://fcm.googleapis.com/fcm/send";
  3. StringRequest myReq = new StringRequest(Request.Method.POST,url,
  4. new Response.Listener<String>() {
  5. @Override
  6. public void onResponse(String response) {
  7. Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
  8. }
  9. },
  10. new Response.ErrorListener() {
  11. @Override
  12. public void onErrorResponse(VolleyError error) {
  13. Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
  14. }
  15. }) {
  16. @Override
  17. public byte[] getBody() throws com.android.volley.AuthFailureError {
  18. Map<String, Object> rawParameters = new Hashtable();
  19. rawParameters.put("data", new JSONObject(dataValue));
  20. rawParameters.put("to", instanceIdToken);
  21. return new JSONObject(rawParameters).toString().getBytes();
  22. };
  23. public String getBodyContentType()
  24. {
  25. return "application/json; charset=utf-8";
  26. }
  27. @Override
  28. public Map<String, String> getHeaders() throws AuthFailureError {
  29. HashMap<String, String> headers = new HashMap<String, String>();
  30. headers.put("Authorization", "key="+YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE);
  31. headers.put("Content-Type","application/json");
  32. return headers;
  33. }
  34. };
  35. Volley.newRequestQueue(activity).add(myReq);
  36. }

注意,如果您想向topics发送消息,那么可以将参数instanceidtoken更改为/topics/topicname之类的值。对于组,实现是相同的,但您只需要注意参数。检查firebase文档,您可以传递这些参数。如果您遇到任何问题,请告诉我。

展开查看全部
pdsfdshx

pdsfdshx3#

我还在我的原型中使用了直接的设备到设备gcm消息传递。它一直运行得很好。我们没有服务器。我们使用短信/文本交换gcm注册id,然后使用gcm进行通信。我把gcm处理的相关代码放在这里

*发送gcm消息

  1. //Sends gcm message Asynchronously
  2. public class GCM_Sender extends IntentService{
  3. final String API_KEY = "****************************************";
  4. //Empty constructor
  5. public GCM_Sender() {
  6. super("GCM_Sender");
  7. }
  8. //Processes gcm send messages
  9. @Override
  10. protected void onHandleIntent(Intent intent) {
  11. Log.d("Action Service", "GCM_Sender Service Started");
  12. //Get message from intent
  13. String msg = intent.getStringExtra("msg");
  14. msg = "\"" + msg + "\"";
  15. try{
  16. String ControllerRegistrationId = null;
  17. //Check registration id in db
  18. if(RegistrationIdAdapter.getInstance(getApplicationContext()).getRegIds().size() > 0 ) {
  19. String controllerRegIdArray[] = RegistrationIdAdapter.getInstance(getApplicationContext()).getRegIds().get(1);
  20. if(controllerRegIdArray.length>0)
  21. ControllerRegistrationId = controllerRegIdArray[controllerRegIdArray.length-1];
  22. if(!ControllerRegistrationId.equalsIgnoreCase("NULL")){
  23. // 1. URL
  24. URL url = new URL("https://android.googleapis.com/gcm/send");
  25. // 2. Open connection
  26. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  27. // 3. Specify POST method
  28. urlConnection.setRequestMethod("POST");
  29. // 4. Set the headers
  30. urlConnection.setRequestProperty("Content-Type", "application/json");
  31. urlConnection.setRequestProperty("Authorization", "key=" + API_KEY);
  32. urlConnection.setDoOutput(true);
  33. // 5. Add JSON data into POST request body
  34. JSONObject obj = new JSONObject("{\"time_to_live\": 0,\"delay_while_idle\": true,\"data\":{\"message\":" + msg + "},\"registration_ids\":[" + ControllerRegistrationId + "]}");
  35. // 6. Get connection output stream
  36. OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
  37. out.write(obj.toString());
  38. out.close();
  39. // 6. Get the response
  40. int responseCode = urlConnection.getResponseCode();
  41. BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  42. String inputLine;
  43. StringBuffer response = new StringBuffer();
  44. while ((inputLine = in.readLine()) != null){
  45. response.append(inputLine);
  46. }
  47. in.close();
  48. Log.d("GCM getResponseCode:", new Integer(responseCode).toString());
  49. }else{
  50. Log.d("GCM_Sender:","Field REGISTRATION_TABLE is null");
  51. }
  52. }else {
  53. Log.d("GCM_Sender:","There is no Registration ID in DB ,please sync devices");
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. //MessageSender.getInstance().sendMessage(msg, Commands.SMS_MESSAGE);
  58. }
  59. }
  60. //Called when service is no longer alive
  61. @Override
  62. public void onDestroy() {
  63. super.onDestroy();
  64. //Do a log that GCM_Sender service has been destroyed
  65. Log.d("Action Service", "GCM_Sender Service Destroyed");
  66. }
  67. }

*接收gcm消息

  1. public class GCM_Receiver extends WakefulBroadcastReceiver {
  2. public static final String RETRY_ACTION ="com.google.android.c2dm.intent.RETRY";
  3. public static final String REGISTRATION ="com.google.android.c2dm.intent.REGISTRATION";
  4. public SharedPreferences preferences;
  5. //Processes Gcm message .
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. ComponentName comp = new ComponentName(context.getPackageName(),
  9. GCMNotificationIntentService.class.getName());
  10. //Start GCMNotificationIntentService to handle gcm message asynchronously
  11. startWakefulService(context, (intent.setComponent(comp)));
  12. setResultCode(Activity.RESULT_OK);
  13. /*//Check if DatabaseService is running .
  14. if(!DatabaseService.isServiceRunning) {
  15. Intent dbService = new Intent(context,DatabaseService.class);
  16. context.startService(dbService);
  17. }*/
  18. //Check if action is RETRY_ACTION ,if it is then do gcm registration again .
  19. if(intent.getAction().equals(RETRY_ACTION)) {
  20. String registrationId = intent.getStringExtra("registration_id");
  21. if(TextUtils.isEmpty(registrationId)){
  22. DeviceRegistrar.getInstance().register(context);
  23. }else {
  24. //Save registration id to prefs .
  25. preferences = PreferenceManager.getDefaultSharedPreferences(context);
  26. SharedPreferences.Editor editor = preferences.edit();
  27. editor.putString("BLACKBOX_REG_ID",registrationId);
  28. editor.commit();
  29. }
  30. } else if (intent.getAction().equals(REGISTRATION)) {
  31. }
  32. }
  33. }
  34. //Processes gcm messages asynchronously .
  35. public class GCMNotificationIntentService extends IntentService{
  36. public static final int NOTIFICATION_ID = 1;
  37. private NotificationManager mNotificationManager;
  38. String gcmData;
  39. private final String TAG = "GCMNotificationIntentService";
  40. //Constructor with super().
  41. public GCMNotificationIntentService() {
  42. super("GcmIntentService");
  43. }
  44. //Called when startService() is called by its Client .
  45. //Processes gcm messages .
  46. @Override
  47. protected void onHandleIntent(Intent intent) {
  48. Log.d("GCMNotificationIntentService", "GCMNotificationIntentService Started");
  49. Bundle extras = intent.getExtras();
  50. //Get instance of GoogleCloudMessaging .
  51. GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
  52. //Get gcm message type .
  53. String messageType = gcm.getMessageType(intent);
  54. if (!extras.isEmpty()) {
  55. if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
  56. .equals(messageType)) {
  57. sendNotification("Send error: " + extras.toString());
  58. } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
  59. .equals(messageType)) {
  60. sendNotification("Deleted messages on server: "
  61. + extras.toString());
  62. } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
  63. .equals(messageType)) {
  64. Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
  65. gcmData = extras.getString("message");
  66. Intent actionService = new Intent(getApplicationContext(),Action.class);
  67. actionService.putExtra("data", gcmData);
  68. //start Action service .
  69. startService(actionService);
  70. //Show push notification .
  71. sendNotification("Action: " + gcmData);
  72. //Process received gcmData.
  73. Log.d(TAG,"Received Gcm Message from Controller : " + extras.getString("message"));
  74. }
  75. }
  76. GCM_Receiver.completeWakefulIntent(intent);
  77. }
  78. //Shows notification on device notification bar .
  79. private void sendNotification(String msg) {
  80. mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
  81. Intent notificationIntent = new Intent(this, BlackboxStarter.class);
  82. //Clicking on GCM notification add new layer of app.
  83. notificationIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  84. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
  85. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
  86. this).setSmallIcon(R.drawable.gcm_cloud)
  87. .setContentTitle("Notification from Controller")
  88. .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
  89. .setContentText(msg);
  90. mBuilder.setContentIntent(contentIntent);
  91. mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
  92. //Play default notification
  93. try {
  94. Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  95. Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
  96. r.play();
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. //Called when service is no longer be available .
  102. @Override
  103. public void onDestroy() {
  104. // TODO Auto-generated method stub
  105. super.onDestroy();
  106. Log.d("GCMNotificationIntentService", "GCMNotificationIntentService Destroyed");
  107. }
  108. }
展开查看全部
wecizke3

wecizke34#

警告:有一个非常重要的原因,为什么我们在任何地方都不提及这种方法。这将在每个客户端设备上放置的apk中公开服务器密钥。它可以(因此将)采取从那里,并可能导致滥用您的项目。我强烈建议不要采用这种方法,除非应用程序只能放在自己的设备上弗兰克范帕夫伦
弗兰克的回答是正确的 Firebase 本机不支持设备到设备的消息传递。不过,这里面有一个漏洞。firebase服务器不确定您是从实际服务器发送请求,还是从设备发送请求。
所以你要做的就是发送一个 Post RequestFirebase 的消息服务器以及服务器密钥。请记住,服务器密钥不应该在设备上,但是如果您希望使用firebase消息传递进行设备到设备的消息传递,则没有其他选项。
我使用的是okhttp,而不是调用restapi的默认方式。代码是这样的-

  1. public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
  2. OkHttpClient mClient = new OkHttpClient();
  3. public void sendMessage(final JSONArray recipients, final String title, final String body, final String icon, final String message) {
  4. new AsyncTask<String, String, String>() {
  5. @Override
  6. protected String doInBackground(String... params) {
  7. try {
  8. JSONObject root = new JSONObject();
  9. JSONObject notification = new JSONObject();
  10. notification.put("body", body);
  11. notification.put("title", title);
  12. notification.put("icon", icon);
  13. JSONObject data = new JSONObject();
  14. data.put("message", message);
  15. root.put("notification", notification);
  16. root.put("data", data);
  17. root.put("registration_ids", recipients);
  18. String result = postToFCM(root.toString());
  19. Log.d(TAG, "Result: " + result);
  20. return result;
  21. } catch (Exception ex) {
  22. ex.printStackTrace();
  23. }
  24. return null;
  25. }
  26. @Override
  27. protected void onPostExecute(String result) {
  28. try {
  29. JSONObject resultJson = new JSONObject(result);
  30. int success, failure;
  31. success = resultJson.getInt("success");
  32. failure = resultJson.getInt("failure");
  33. Toast.makeText(getCurrentActivity(), "Message Success: " + success + "Message Failed: " + failure, Toast.LENGTH_LONG).show();
  34. } catch (JSONException e) {
  35. e.printStackTrace();
  36. Toast.makeText(getCurrentActivity(), "Message Failed, Unknown error occurred.", Toast.LENGTH_LONG).show();
  37. }
  38. }
  39. }.execute();
  40. }
  41. String postToFCM(String bodyString) throws IOException {
  42. RequestBody body = RequestBody.create(JSON, bodyString);
  43. Request request = new Request.Builder()
  44. .url(FCM_MESSAGE_URL)
  45. .post(body)
  46. .addHeader("Authorization", "key=" + SERVER_KEY)
  47. .build();
  48. Response response = mClient.newCall(request).execute();
  49. return response.body().string();
  50. }

我希望firebase将来会有更好的解决方案。但在那之前,我认为这是唯一的办法。另一种方式是发送主题消息或组消息。但这不在问题的范围之内。
更新:
jsonarray的定义如下-

  1. JSONArray regArray = new JSONArray(regIds);

regids是注册id的字符串数组,您要将此消息发送到。请记住,注册ID必须始终位于一个数组中,即使您希望它发送给单个收件人。

展开查看全部
ltqd579y

ltqd579y5#

根据上更新的新文件 October 2, 2018 您必须按以下方式发送邮寄请求

  1. https://fcm.googleapis.com/fcm/send
  2. Content-Type:application/json
  3. Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA //Server key
  4. {
  5. "to": "sent device's registration token",
  6. "data": {
  7. "hello": "message from someone",
  8. }
  9. }

获取设备的注册令牌 FirebaseMessagingService 和覆盖 onNewToken(String token) 有关更多信息,请参阅文档https://firebase.google.com/docs/cloud-messaging/android/device-group

相关问题