Вот что у меня сработало:
public class FCMNotification {
public final static String AUTH_KEY_FCM = "your_key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(String DeviceIdKey) throws Exception {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + authKey);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject data = new JSONObject();
data.put("to", DeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", "FCM Notificatoin Title"); // Notification title
info.put("text", "Hello First Test notification"); // Notification body
data.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
FCMNotification.pushFCMNotification("token_of_the_device");
}
}
Также они внесли некоторые изменения в получение сообщения:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
/**
* Service that is responsible for receiving firebase messages and handling
them
*
* @author filip.trajkovski
* @version 1.0
* @since 1.0
*/
public class FirebaseListenerService extends FirebaseMessagingService {
public static final String TAG = FirebaseListenerService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Message received from: " + remoteMessage.getFrom());
Log.d(TAG,"----- THIS IS THE MESSAGE RECEIVED ------");
Log.d(TAG, "Message: " + remoteMessage.getNotification().getBody());
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
}
}
И добавьте это в свой AndroidManifest. xml:
<service android:name=".cloud.msg.FirebaseListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>