Created
June 18, 2016 00:49
-
-
Save jerryOkafor/cfa7ab28c6af7992616341a2ff2902a4 to your computer and use it in GitHub Desktop.
This will show u how to send the messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
For step-by-step instructions on connecting your Android application to this backend module, | |
see "App Engine Backend with Google Cloud Messaging" template documentation at | |
https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints | |
*/ | |
package com.dotdex.squattn; | |
import com.dotdex.squattn.models.RegistrationRecord; | |
import com.dotdex.squattn.util.Utility; | |
import com.google.android.gcm.server.Constants; | |
import com.google.android.gcm.server.Message; | |
import com.google.android.gcm.server.Result; | |
import com.google.android.gcm.server.Sender; | |
import com.google.api.server.spi.config.Api; | |
import com.google.api.server.spi.config.ApiMethod; | |
import com.google.api.server.spi.config.ApiNamespace; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import javax.inject.Named; | |
import static com.dotdex.squattn.OfyService.ofy; | |
/** | |
* An endpoint to send messages to devices registered with the backend | |
* <p/> | |
* For more information, see | |
* https://developers.google.com/appengine/docs/java/endpoints/ | |
* <p/> | |
* NOTE: This endpoint does not use any form of authorization or | |
* authentication! If this app is deployed, anyone can access this endpoint! If | |
* you'd like to add authentication, take a look at the documentation. | |
*/ | |
@Api( | |
name = "messaging", | |
version = "v1", | |
namespace = @ApiNamespace( | |
ownerDomain = "squattn.dotdex.com", | |
ownerName = "squattn.dotdex.com", | |
packagePath="" | |
) | |
) | |
public class MessagingEndpoint { | |
private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName()); | |
/** 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 | |
*/ | |
@ApiMethod(name = "sendMessage") | |
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("type",Utility.PUSH_NOTIFICATION) | |
.addData("message", message) | |
.build(); | |
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(100).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 data store | |
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 data store"); | |
// 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); | |
} | |
} | |
} | |
} | |
/** | |
* Send a notification to the owner of the space | |
* | |
* @param name The message to send | |
*/ | |
@ApiMethod(name = "sendRequestNotification") | |
public void sendRequestNotification( | |
@Named("requesterName") String name, | |
@Named("requestFrom") String requestFrom, | |
@Named("requestKey") String requestKey, | |
@Named("requestTo") String requestTo, | |
@Named("spaceKey") String spaceKey, | |
@Named("spaceNum") String spaceNum, | |
@Named("spaceLoc") String spaceLoc | |
) throws IOException{ | |
Sender sender = new Sender(API_KEY); | |
String big_msg = name +" wants to rent your parking space located @ "+spaceLoc+" with SQN Number *"+spaceNum+"*"; | |
String small_msg = name + " wants to rent your parking space!"; | |
Message msg = new Message.Builder() | |
.addData("type", Utility.GCM_MSG_TYPE_NOTIFICATION) | |
.addData("small_msg",small_msg) | |
.addData("big_msg", big_msg) | |
.addData("name",name) | |
.addData("request_from",requestFrom) | |
.addData("request_to",requestTo) | |
.addData("space_key",spaceKey) | |
.addData("space_num",spaceNum) | |
.addData("space_loc",spaceLoc) | |
.addData("request_key",requestKey) | |
.build(); | |
RegistrationRecord record = ofy().load().type(RegistrationRecord.class).filter("uId ==", requestTo).first().now(); | |
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 data store | |
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 data store"); | |
// if the device is no longer registered with Gcm, remove it from the data store | |
ofy().delete().entity(record).now(); | |
} else { | |
log.warning("Error when sending message : " + error); | |
} | |
} | |
} | |
@ApiMethod(name = "sendRequestConfirmationNotification") | |
public void sendRequestConfirmationNotification( | |
@Named("requesterName") String name, | |
@Named("requestFrom") String requestFrom, | |
@Named("requestKey") String requestKey, | |
@Named("requestTo") String requestTo, | |
@Named("spaceKey") String spaceKey, | |
@Named("spaceNum") String spaceNum, | |
@Named("spaceLoc") String spaceLoc, | |
@Named("confirmationType") String type | |
) throws IOException { | |
Sender sender = new Sender(API_KEY); | |
StringBuilder msgBuilder = new StringBuilder(); | |
msgBuilder.append(name + ", Your request to rent a parking space located @ " + spaceLoc + " with SQN Number *" + spaceNum + "* " + | |
"has being"); | |
String small_msg = ""; | |
if (type.equals(Utility.CONFIRMATION_TYPE_APPROVED)) | |
{ | |
msgBuilder.append(" approved by the space owner and manager. You can now proceed and make use of the space"); | |
small_msg = "Your request for a paring space has been approved!"; | |
}else if (type.equals(Utility.CONFIRMATION_TYPE_DISAPPROVED)){ | |
msgBuilder.append(" disapproved by the space owner and manager. Please try again!"); | |
small_msg = " Your request for a paring space has been disapproved!"; | |
} | |
Message msg = new Message.Builder() | |
.addData("type", Utility.GCM_MSG_TYPE_APPROVAL_NOTIFICATION) | |
.addData("small_msg", small_msg) | |
.addData("big_msg", msgBuilder.toString()) | |
// .addData("requester_name", name)//shall be used to decrease his credit | |
.addData("request_from", requestFrom)//shall be used to decrease his credit | |
.addData("request_from", requestTo) //shall be used to increment his credit | |
// .addData("space_key", spaceKey) //shall be used to to sent the toggle the request status | |
// .addData("space_num", spaceNum) // | |
.addData("request_key", requestKey) | |
.build(); | |
RegistrationRecord record = ofy().load().type(RegistrationRecord.class).filter("uId ==", requestFrom).first().now(); | |
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 data store | |
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); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment