R
R
retr02017-06-06 19:38:14
Java
retr0, 2017-06-06 19:38:14

Firebase cloud messaging not working, how to fix?

There is an application, I integrated FCM into it, tested sending notifications through the Firebase console - it works fine. Then I wrote a simple program to send notifications outside the console, there are no errors in the logs, but notifications are not sent.
A class inherited from Sender to send a notification to the endpoint:

import com.google.android.gcm.server.Sender;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class FCMSender extends Sender {

    public FCMSender(String key) {
        super(key);
    }

    @Override
    protected HttpURLConnection getConnection(String url) throws IOException {
        String fcmUrl = "https://fcm.googleapis.com/fcm/send";
        return (HttpURLConnection) new URL(fcmUrl).openConnection();
    }
}

Main class:
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class MainServer {

    public static void main(String[] args) {

        //Ключ сервера из Настройки проекта/Cloud messaging
        final String ServerKey = "AAAANe_-GCc:APA91bHQftyF95qsZYu0ELS83I390w-TxWsARDJ-nEHb5bKQ1JdMF0s045bUTefo_D22ATpRy5vlJ1Lla7o0vcTDs6fLY_BhyutQzi9B_zOUBLtCekDrf8pt1kb3z1ur8fwfDHWm1aPb";

        Thread t = new Thread() {
            public void run() {

                try {
                    Sender sender = new FCMSender(ServerKey);
                    Message message = new Message.Builder()
                            .collapseKey("message")
                            .timeToLive(3)
                            .delayWhileIdle(true)
                            .addData("message", "Тест")
                            .build();
                    //Токен пользователя
                    Result result = sender.send(message, "fEJ4nH0yHyQ:APA91bEyNXcTauN6WHl3pZmELHSBhtqlsIerdR30WvkZu3dhJFHoZyQRJjP9bh6UIjR9RQZlezb_3qKKqv3hoP96UMK5tPTUH9PcbPxGJG_NGd0-o-Jk7aqzMRGl25x8fpt0hqie1fOI", 1);
                    System.out.println("Result" + result.toString());
                } catch (Exception e){
                    System.out.println("Sending error.");
                }
            }
        };

        t.start();
        try {
            t.join();
        } catch (Exception e){
            System.out.println("Join error.");
        }
    }
}

The logs write the following:
Result[ messageId=0:1496767045494310%810639f938eb0007 ]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question