How to send a Cloud Firebase message from a Google App Engine application

How can you send a Cloud Firebase message from the Google App Engine / Cloud Endpoints app?

Android Studio automatically generates the following code to send Google Cloud Message. Although you can use the same code to send FCM, you cannot set a โ€œnotificationโ€ or โ€œpriorityโ€ or something similar that uses the new FCM.

Is there a gradle import for this or an example of using Firebase Cloud Messaging inside the App Engine application so that you can easily do things like set a message, notification, priority, etc.?

This is what the Android Studio Cloud endpoint is currently creating for you:

// Gradle dependency: compile 'com.google.gcm:gcm-server:1.0.0' /** * Api Keys can be obtained from the google cloud console */ private static final String API_KEY = System.getProperty("gcm.api.key"); /** * Send to the first 10 devices (You can modify this to send to any number of devices or a specific device) * * @param message The message to send */ public void sendMessage(@Named("message") String message) throws IOException { if (message == null || message.trim().length() == 0) { log.warning("Not sending message because it is empty"); return; } // crop longer messages if (message.length() > 1000) { message = message.substring(0, 1000) + "[...]"; } Sender sender = new Sender(API_KEY); Message msg = new Message.Builder().addData("message", message).build(); List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list(); for (RegistrationRecord record : records) { Result result = sender.send(msg, record.getRegId(), 5); if (result.getMessageId() != null) { log.info("Message sent to " + record.getRegId()); String canonicalRegId = result.getCanonicalRegistrationId(); if (canonicalRegId != null) { // if the regId changed, we have to update the datastore log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId); record.setRegId(canonicalRegId); ofy().save().entity(record).now(); } } else { String error = result.getErrorCodeName(); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore"); // if the device is no longer registered with Gcm, remove it from the datastore ofy().delete().entity(record).now(); } else { log.warning("Error when sending message : " + error); } } } } 
+5
source share
1 answer

There is currently no Firebase client to send messages from the application server. You should simply send raw HTTP requests with JSON payloads from your endpoint using the protocol registered here . The server reference shows which options you can use, including priority.

+5
source

All Articles