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

后端 未结 13 1935
Happy的楠姐
Happy的楠姐 2020-11-22 01:47

After searching the docs I could not find any info on how to send device to device messages using FCM without the use of an external server.

For example, if I was cr

13条回答
  •  梦谈多话
    2020-11-22 02:16

    You can use Retrofit. Subscribe devices to topic news. Send notification from one device to other.

    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 call2 = firebaseAPI.sendMessage(new Message("topic or deviceID", notifydata));
    
        call2.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
    
                Log.d("Response ", "onResponse");
                t1.setText("Notification sent");
    
            }
    
            @Override
            public void onFailure(Call call, Throwable t) {
                Log.d("Response ", "onFailure");
                t1.setText("Notification failure");
            }
        });
    }
    

    POJOs

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

    and

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

    and FirebaseAPI

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

提交回复
热议问题