How is Google Cloud Messaging to send messages?

0

Someone knows how they use GCM (Google Cloud Messaging), applications like WhatsAuto or similar, to send messages to my WhatsApp contacts, I got the WhatsAuto message and I saw that they have these permissions.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.android.vending.BILLING"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.future.whatslol.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.future.whatslol.permission.C2D_MESSAGE"/>

But I would like to know how you use them to send messages to my WhatsApp contacts

    
asked by Abisur Diaz Ramirez 29.07.2018 в 22:51
source

1 answer

0

Google cloud messaging has been replaced by Firebase Cloud Messaging (FCM), if what you are occupying is sending a push notification to certain devices you must use the FCM service. In essence, you set up an account in Firebase, activate the Cloud Messaging service, and configure it in your app.

Since there is no sdk-android-firebase method that sent notifications, you must set up a custom back-end, or if you only want to send notifications as a kind of chat, you can use the FCM REST API to send a notification by POST method.

He left you a link to the official firebase documentation, so you can configure the service in your app, and attach an example code on how to use Okhttp to send a "device to device" notification via POST.

link

I also share an ALvarezTech tutorial, without a doubt, explains how to install the service very simple. link

//First you need to add a OkhttpCliente dependency on your App Gradle File.
//You can find them in the next URL: https://github.com/square/okhttp


private void sendNotification(String regToken) {
        final String regToken2 = regToken;
        final String LEGACY_SERVER_KEY = "AIzaSyD6QliPCOBwISMu35_r_MzxSY6uc0gLd7s"; //Replace with your FCM Server Key
        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 its a notification");
                        dataJson.put("title","hello world");
                        dataJson.put("sound", "/uri/audio/listen.mp3");
                        json.put("notification",dataJson);

                        json.put("to",regToken2);
                        RequestBody body = RequestBody.create(JSON, json.toString());
                        Request request = new Request.Builder()
                                .header("Authorization","key="+ 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();

    }

If you get stuck in something, I can help you.

    
answered by 30.07.2018 в 01:30