首页 文章

如何使用Firebase Cloud 消息传送设备到设备消息?

提问于
浏览
59

搜索文档后,我找不到任何有关如何使用FCM将设备发送到设备消息而不使用外部服务器的信息 .

例如,如果我正在创建一个聊天应用程序,我需要向用户发送有关未读消息的推送通知,因为它们不会一直在线,我不能在后台始终连接到持久服务实时数据库,因为这太资源了 .

那么当某个用户“B”向他/她发送聊天消息时,如何向用户“A”发送推送通知?我是否需要外部服务器,还是仅使用Firebase服务器?

13 回答

  • 3

    Google Cloud 端功能现在可以在没有应用服务器的情况下从设备到设备发送推送通知 .

    From the relevant page关于Google Cloud功能:

    开发人员可以使用 Cloud 功能让用户参与并了解有关应用的相关信息 . 例如,考虑一个允许用户在应用程序中关注彼此活动的应用程序 . 在这样的应用程序中,由实时数据库写入存储新关注者的功能可以创建Firebase Cloud 消息传递(FCM)通知,以便让相应的用户知道他们已经获得了新的关注者 . 示例:该函数在写入存储关注者的实时数据库路径时触发 . 该功能组成要通过FCM发送的消息 . FCM将通知消息发送到用户的设备 .

    Here is a demo project用于使用Firebase和Google Cloud Functions发送设备到设备推送通知 .

  • 30

    您可以使用Retrofit . 订阅设备到主题新闻 . 从一个设备向另一个设备发送通知 .

    public void onClick(View view) {
    
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
    
                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", "key=legacy server key from FB console"); // <-- this is the important line
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    
        httpClient.addInterceptor(logging);
        OkHttpClient client = httpClient.build();
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://fcm.googleapis.com")//url of FCM message server
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
                .build();
    
        // prepare call in Retrofit 2.0
        FirebaseAPI firebaseAPI = retrofit.create(FirebaseAPI.class);
    
        //for messaging server
        NotifyData notifydata = new NotifyData("Notification title","Notification body");
    
    Call<Message> call2 = firebaseAPI.sendMessage(new Message("topic or deviceID", notifydata));
    
        call2.enqueue(new Callback<Message>() {
            @Override
            public void onResponse(Call<Message> call, Response<Message> response) {
    
                Log.d("Response ", "onResponse");
                t1.setText("Notification sent");
    
            }
    
            @Override
            public void onFailure(Call<Message> call, Throwable t) {
                Log.d("Response ", "onFailure");
                t1.setText("Notification failure");
            }
        });
    }
    

    的POJO

    public class Message {
    String to;
    NotifyData notification;
    
    public Message(String to, NotifyData notification) {
        this.to = to;
        this.notification = notification;
    }
    
    }
    

    public class NotifyData {
    String title;
    String body;
    
    public NotifyData(String title, String body ) {
    
        this.title = title;
        this.body = body;
    }
    
    }
    

    和FirebaseAPI

    public interface FirebaseAPI {
    
    @POST("/fcm/send")
    Call<Message> sendMessage(@Body Message message);
    
    }
    
  • 1

    Google Cloud 端功能现在可以在没有应用服务器的情况下从设备到设备发送推送通知 . 我已经制作了 Cloud 功能,这是在数据库中添加新消息时触发的

    这是 node.js 代码

    'use strict';
    
    const functions = require('firebase-functions');
    const admin = require('firebase-admin'); admin.initializeApp();
    
    exports.sendNotification = functions.database.ref('/conversations/{chatLocation}/{messageLocation}')
      .onCreate((snapshot, context) => {
          // Grab the current value of what was written to the Realtime Database.
          const original = snapshot.val();
    
           const toIDUser = original.toID;
           const isGroupChat = original.isGroupChat;
    
           if (isGroupChat) {
           const tokenss =  admin.database().ref(`/users/${toIDUser}/tokens`).once('value').then(function(snapshot) {
    
    // Handle Promise
           const tokenOfGroup = snapshot.val()
    
          // get tokens from the database  at particular location get values 
           const valuess = Object.keys(tokenOfGroup).map(k => tokenOfGroup[k]);
    
         //console.log(' ____________ddd((999999ddd_________________ ' +  valuess );
        const payload = {
           notification: {
                     title:   original.senderName + " :- ",
                     body:    original.content
        }
      };
    
      return admin.messaging().sendToDevice(valuess, payload);
    
    
    
    }, function(error) {
    
      console.error(error);
    });
    
           return ;
              } else {
              // get token from the database  at particular location
                    const tokenss =  admin.database().ref(`/users/${toIDUser}/credentials`).once('value').then(function(snapshot) {
                    // Handle Promise
      // The Promise was "fulfilled" (it succeeded).
    
         const credentials = snapshot.val()
    
    
    
        // console.log('snapshot ......snapshot.val().name****^^^^^^^^^^^^kensPromise****** :- ', credentials.name);
         //console.log('snapshot.....****snapshot.val().token****^^^^^^^^^^^^kensPromise****** :- ', credentials.token);
    
    
         const deviceToken = credentials.token;
    
        const payload = {
           notification: {
                     title:   original.senderName + " :- ",
                     body:    original.content
        }
      };
    
      return admin.messaging().sendToDevice(deviceToken, payload);
    
    
    }, function(error) {
    
      console.error(error);
    });
    
    
              }
    
    
    
    
    
      return ;
    
    
        });
    
  • 0

    1)订阅相同的主题名称,例如:

    • ClientA.subcribe("to/topic_users_channel")

    • ClientB.subcribe("to/topic_users_channel")

    2)在应用程序内发送消息

    GoogleFirebase : How-to send topic messages

  • 2

    最简单的方法:

    void sendFCMPush(String msg,String token) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
    
                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", "key="+Const.FIREBASE_LEGACY_SERVER_KEY); // <-- this is the important line
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    
        httpClient.addInterceptor(logging);
        OkHttpClient client = httpClient.build();
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://fcm.googleapis.com/")//url of FCM message server
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
                .build();
    
        // prepare call in Retrofit 2.0
        FirebaseAPI firebaseAPI = retrofit.create(FirebaseAPI.class);
    
        //for messaging server
        NotifyData notifydata = new NotifyData("Chatting", msg);
    
        Call<Message> call2 = firebaseAPI.sendMessage(new Message(token, notifydata));
    
        call2.enqueue(new Callback<Message>() {
            @Override
            public void onResponse(Call<Message> call, retrofit2.Response<Message> response) {
                Log.e("#@ SUCCES #E$#", response.body().toString());
            }
    
            @Override
            public void onFailure(Call<Message> call, Throwable t) {
    
                Log.e("E$ FAILURE E$#", t.getMessage());
            }
        });
    }
    

    创建类来制作对象:

    public class Message {
    String to;
    NotifyData data;
    
    public Message(String to, NotifyData data) {
        this.to = to;
        this.data = data;
    }
    }
    

    创建类来制作对象:

    public class Notification {
    String title;
    String message;
    enter code here`enter code here`
    public Notification(String title, String message) {
        this.title = title;
        this.message = message;
    }
    }
    
  • -2

    如果您有要向其发送通知的设备的fcm(gcm)标记 . 这只是发送通知的发布请求 .

    https://github.com/prashanthd/google-services/blob/master/android/gcm/gcmsender/src/main/java/gcm/play/android/samples/com/gcmsender/GcmSender.java

  • 0

    在我的情况下,我使用retrofit与此类消息:

    public class Message {
    
        private String to;
        private String collapseKey;
        private Notification notification;
        private Data data;
    
        public Message(String to, String collapseKey, Notification notification, Data data) {
            this.to = to;
            this.collapseKey = collapseKey;
            this.notification = notification;
            this.data = data;
        }
    

    数据

    public class Data {
    
        private String body;
        private String title;
        private String key1;
        private String key2;
    
        public Data(String body, String title, String key1, String key2) {
            this.body = body;
            this.title = title;
            this.key1 = key1;
            this.key2 = key2;
        }
    }
    

    通知

    public class Notification {
    
        private String body;
        private String title;
    
        public Notification(String body, String title) {
            this.body = body;
            this.title = title;
        }
    }
    

    这个电话

    private void sentToNotification() {
    
    
                String to = "YOUR_TOKEN";
                String collapseKey = "";
                Notification notification = new Notification("Hello bro", "title23");
                Data data = new Data("Hello2", "title2", "key1", "key2");
                Message notificationTask = new Message(to, collapseKey, notification, data);
    
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://fcm.googleapis.com/")//url of FCM message server
                        .addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
                        .build();
    
                ServiceAPI api = new retrofit.create(ServiceAPI.class);
    
                Call<Message> call = api .sendMessage("key=YOUR_KEY", notificationTask);
    
                call.enqueue(new Callback<Message>() {
                    @Override
                    public void onResponse(Call<Message> call, retrofit2.Response<Message> response) {
                        Log.d("TAG", response.body().toString());
                    }
    
                    @Override
                    public void onFailure(Call<Message> call, Throwable t) {
    
                        Log.e("TAG", t.getMessage());
                    }
                });
            }
    

    我们的ServiceAPi

    public interface ServiceAPI {
        @POST("/fcm/send")
        Call<Message> sendMessage(@Header("Authorization") String token, @Body Message message);
    }
    
  • 0

    UPDATE: 现在可以使用firebase Cloud 功能作为处理推送通知的服务器 . 看看他们的文件here

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

    根据文档,您必须实现服务器以处理设备到设备通信中的推送通知 .

    在编写使用Firebase Cloud Messaging的客户端应用程序之前,您必须拥有符合以下条件的应用程序服务器:...您需要确定要使用哪个FCM连接服务器协议来启用应用程序服务器与FCM连接服务器交互 . 请注意,如果要使用客户端应用程序的上游消息传递,则必须使用XMPP . 有关此内容的更详细讨论,请参阅选择FCM连接服务器协议 .

    如果您只需要从服务器向用户发送基本通知 . 您可以使用无服务器解决方案Firebase Notifications .

    查看FCM和Firebase通知之间的比较:https://firebase.google.com/support/faq/#messaging-difference

  • 1

    我想出这样的: -
    使用链接“https://fcm.googleapis.com/fcm/send”发出HTTP POST请求,其中包含所需的标头和数据HERE .
    Constants.LEGACY_SERVER_KEY是本地类变量,您可以在Firebase项目设置 - > Cloud 消息传递 - >旧版服务器密钥中找到它 . 您需要在下面引用的代码段HERE.中传递设备注册令牌(reg_token)
    但是,您需要okhttp库依赖项才能使此代码段工作 .

    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    private void sendNotification(final String regToken) {
        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",regToken);
                    RequestBody body = RequestBody.create(JSON, json.toString());
                    Request request = new Request.Builder()
                            .header("Authorization","key="+Constants.LEGACY_SERVER_KEY)
                            .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;
            }
        }.execute();
    
    }
    

    如果你想发送消息到特定主题,请像这样替换json中的'regToken'

    json.put("to","/topics/foo-bar")
    

    最后不要忘记在AndroidManifest.xml中添加INTERNET权限 .

    IMPORTANT : - 使用上面的代码表示您的服务器密钥驻留在客户端应用程序中 . 这是危险的,因为有人可以深入了解您的应用程序并获取服务器密钥以向您的用户发送恶意通知 .

  • 1

    是的,没有任何服务器就可以做到这一点 . 您可以创建设备组客户端,然后在组中交换消息 . 但是有一些限制:

    • 您必须在设备上使用相同的Google帐户

    • 您无法发送高优先级消息

    参考:Firebase doc参见"Managing device groups on Android client apps"部分

  • 15

    所以我在这里有个主意 . 请参阅:如果FCM以及GCM具有http请求的endpoit,我们可以在其中发送json的帖子我们的消息数据,包括我们希望传递此消息的设备令牌 .

    那么为什么不向Firebase服务器发送帖子并将此通知发送给用户B?你明白 ?

    因此,您发送消息并与通话信息聊天,以确保在用户在后台使用您的应用时发送通知 . 我也很快就需要它,我稍后会测试 . 你怎么说的?

  • 1

    你可以使用Volly Jsonobject请求来做到这一点....

    首先按照此步骤操作:

    1 复制旧版服务器密钥并将其存储为 Legacy_SERVER_KEY

    旧版服务器密钥

    你可以在图片中看到如何获得

    2 你需要Volley依赖

    编译'com.mcxiaoke.volley:library:1.0.19'

    enter image description here

    发送推送代码: -

    private void sendFCMPush() {
    
                String Legacy_SERVER_KEY = YOUR_Legacy_SERVER_KEY;
                String msg = "this is test message,.,,.,.";
                String title = "my title";
                String token = FCM_RECEIVER_TOKEN;
    
                JSONObject obj = null;
            JSONObject objData = null;
            JSONObject dataobjData = null;
    
            try {
                obj = new JSONObject();
                objData = new JSONObject();
    
                objData.put("body", msg);
                objData.put("title", title);
                objData.put("sound", "default");
                objData.put("icon", "icon_name"); //   icon_name image must be there in drawable
                objData.put("tag", token);
                objData.put("priority", "high");
    
                dataobjData = new JSONObject();
                dataobjData.put("text", msg);
                dataobjData.put("title", title);
    
                obj.put("to", token);
                //obj.put("priority", "high");
    
                obj.put("notification", objData);
                obj.put("data", dataobjData);
                Log.e("!_@rj@_@@_PASS:>", obj.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
                JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, Constants.FCM_PUSH_URL, obj,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Log.e("!_@@_SUCESS", response + "");
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("!_@@_Errors--", error + "");
                            }
                        }) {
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Authorization", "key=" + Legacy_SERVER_KEY);
                        params.put("Content-Type", "application/json");
                        return params;
                    }
                };
                RequestQueue requestQueue = Volley.newRequestQueue(this);
                int socketTimeout = 1000 * 60;// 60 seconds
                RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
                jsObjRequest.setRetryPolicy(policy);
                requestQueue.add(jsObjRequest);
    }
    

    只需致电 sendFCMPush() ;

  • 0

    您可以使用firebase实时数据库来执行此操作 . 您可以创建用于存储聊天的数据结构,并为两个用户的会话线程添加观察者 . 它仍然是设备 - 服务器 - 设备架构,但在这种情况下,开发人员没有额外的服务器 . 这使用了firebase服务器 . 您可以在这里查看教程(忽略UI部分,尽管这也是聊天UI框架的一个很好的起点) .

    Firebase Realtime Chat

相关问题