本文整理匯總了Java中com.google.android.gcm.server.Result.getErrorCodeName方法的典型用法代碼示例。如果您正苦於以下問題:Java Result.getErrorCodeName方法的具體用法?Java Result.getErrorCodeName怎麽用?Java Result.getErrorCodeName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.android.gcm.server.Result
的用法示例。
在下文中一共展示了Result.getErrorCodeName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
public void sendMessage(List<String> registrationIds, Message message) throws IOException {
mLog.info("Sending to " + registrationIds.size() + " clients message " + message);
for (String registrationId : registrationIds) {
// sendNoReply otherwise a device switched off will receive the message the same
Result result = mSender.sendNoRetry(message, registrationId);
if (result.getMessageId() != null) {
mLog.info("Message sent to " + registrationId);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
mLog.info("Registration Id changed for " + registrationId + " updating to " + canonicalRegId);
mRegistrationDao.updateRegistrationId(registrationId, canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
mLog.warning("Registration Id " + registrationId + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
mRegistrationDao.removeByRegistrationId(registrationId);
} else {
mLog.warning("Error when sending message : " + error);
}
}
}
}
示例2: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* 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);
}
}
}
}
示例3: run
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
public void run() {
Sender sender = new Sender(SENDER_ID);
Message message = new Message.Builder()
// If multiple messages are sent while device is offline,
// only receive the latest message is received.
.collapseKey(COLLAPSE_KEY + "_" + p.getId())
// TTL = 6 hours (if scheduled at 9h, push is received until
// 15h)
.timeToLive(21600).delayWhileIdle(true).addData("title", p.getTitle())
.addData("numberOfRestaurants", restaurantsForPushCount.toString())
.addData("longitude", String.valueOf(p.getLongitude()))
.addData("latitude", String.valueOf(p.getLatitude())).addData("radius", String.valueOf(p.getRadius()))
.addData("kitchenTypeIds", pushKitchenTypeIds.toString()).addData("pushId", String.valueOf(p.getId()))
.build();
try {
Result result = sender.send(message, p.getGcmToken(), NUMBER_OF_RETRIES);
if (result.getErrorCodeName() == null) {
LOGGER.info(
LogUtils.getDefaultSchedulerMessage(Thread.currentThread().getStackTrace()[1].getMethodName(),
"GCM Notification was sent successfully: " + message.toString()));
} else if (result.getErrorCodeName().equals("InvalidRegistration")) {
pushRepo.delete(p);
LOGGER.error(LogUtils.getErrorMessage(Thread.currentThread().getStackTrace()[1].getMethodName(),
"GCM Token invalid: Push-Notification is deleted."));
} else {
LOGGER.error(LogUtils.getErrorMessage(Thread.currentThread().getStackTrace()[1].getMethodName(),
"Error occurred while sending push notification :" + result.getErrorCodeName()));
}
} catch (Exception e) {
LOGGER.error(LogUtils.getExceptionMessage(Thread.currentThread().getStackTrace()[1].getMethodName(), e));
}
return;
}
示例4: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* 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: sendGcmMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
@Override
public void sendGcmMessage(UserRecord user, Message message) throws IOException {
boolean updateUser = false;
// Use iterator instead of foreach to prevent ConcurrentModificationException
ListIterator<String> iterator = user.getDevices().listIterator();
while (iterator.hasNext()) {
String device = iterator.next();
Result result = sender.send(message, device, 1);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update it
iterator.remove();
iterator.add(canonicalRegId);
updateUser = true;
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// if the device is no longer registered with Gcm, remove it
iterator.remove();
updateUser = true;
}
}
}
if (updateUser) {
userDAO.save(user);
}
}
示例6: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* 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.getToken(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getToken());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getToken() + " updating to " + canonicalRegId);
record.setToken(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getToken() + " 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);
}
}
}
}
示例7: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* Send to the first 10 devices
*
* @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);
}
}
}
}
示例8: doSendViaGcm
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* Sends the message using the Sender object to the registered device.
*
* @param message
* the message to be sent in the GCM ping to the device.
* @param sender
* the Sender object to be used for ping,
* @param deviceInfo
* the registration id of the device.
* @return Result the result of the ping.
*/
private static Result doSendViaGcm(String message, Sender sender,
DBObject deviceInfo, DBCollection coll) throws IOException {
// Trim message if needed.
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
// This message object is a Google Cloud Messaging object, it is NOT
// related to the MessageData class
Message msg = new Message.Builder().addData("message", message).build();
Result result = sender.send(msg, (String) deviceInfo.get("_id"),
5);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
coll.save(deviceInfo);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
coll.remove(deviceInfo);
}
}
return result;
}
示例9: sendGcmNotification
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
void sendGcmNotification(final PatientEntity p, final String title, final String msg) {
if (!p.isGcmUser()) {
throw new IllegalStateException(
"User is not an Android user. We should never attempt to send a GCM message if we don't have a valid GCM registration id");
}
final String regId = p.getProperties().get("c2dmRegistrationId");
try {
Sender sender = new Sender(this.gcmAuthKey);
Message message = new Message.Builder().addData("title", title).addData("message", msg)
.addData("timestamp", String.valueOf(System.currentTimeMillis())).build();
Result result = sender.send(message, regId, 5);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update
// database
getLog().debug(
"Google indicates that the device has more than one registration id. Update to the correct one");
p.getProperties().put("c2dmRegistrationId", canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister
// database
getLog().debug(
"Google indicates that the registration id has been removed from the device. Remove from our database.");
p.getProperties().remove("c2dmRegistrationId");
}
}
} catch (final IOException e) {
e.printStackTrace();
getLog().warn("Could not send push through the GCM network. Error was: " + e.getMessage());
}
}
示例10: push
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
public final void push(String registrationId, String apiKey,
Map<String, Object> args, boolean delayWhileIdle)
throws IOException {
Sender sender = new Sender(apiKey);
Message.Builder builder = new Message.Builder();
builder.timeToLive(60 * 60).collapseKey("MAGIC_STRING")
.delayWhileIdle(delayWhileIdle);
for (String key : args.keySet()) {
builder.addData(key, "" + args.get(key));
}
Message message = builder.build();
Result result = sender.send(message, registrationId, 5);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
System.out
.println("same device has more than on registration ID: update database");
} else {
System.out.println("ok");
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister
// database
System.out
.println("application has been removed from device - unregister database");
}
System.out.println("ok 2: " + error);
}
}
示例11: sendSingleMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
private void sendSingleMessage(String regId, HttpServletResponse resp) {
logger.info("Sending message to device " + regId);
Message message = createMessage();
Result result;
try {
result = sender.sendNoRetry(message, regId);
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception posting " + message, e);
taskDone(resp);
return;
}
if (result == null) {
retryTask(resp);
return;
}
if (result.getMessageId() != null) {
logger.info("Succesfully sent message to device " + regId);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration id: update it
logger.finest("canonicalRegId " + canonicalRegId);
Datastore.updateRegistration(regId, canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister it
Datastore.unregister(regId);
} else {
logger.severe("Error sending message to device " + regId
+ ": " + error);
}
}
}
示例12: doSendViaGcm
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
private static Result doSendViaGcm(String message, Sender sender,
DeviceInfo deviceInfo) throws IOException {
// Trim message if needed.
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
// This message object is a Google Cloud Messaging object, it is NOT
// related to the MessageData class
Message msg = new Message.Builder().addData("message", message).build();
Result result = sender.send(msg, deviceInfo.getDeviceRegistrationID(),
5);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
endpoint.removeDeviceInfo(deviceInfo.getDeviceRegistrationID());
deviceInfo.setDeviceRegistrationID(canonicalRegId);
endpoint.insertDeviceInfo(null, deviceInfo);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
endpoint.removeDeviceInfo(deviceInfo.getDeviceRegistrationID());
}
}
return result;
}
示例13: doSendViaGcm
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* Sends the message using the Sender object to the registered device.
*
* @param message the message to be sent in the GCM ping to the device.
* @param sender the Sender object to be used for ping,
* @param deviceInfo the registration id of the device.
* @return Result the result of the ping.
*/
private static Result doSendViaGcm(String message, Sender sender,
DeviceInfo deviceInfo) throws IOException {
// Trim message if needed.
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
// This message object is a Google Cloud Messaging object, it is NOT
// related to the MessageData class
Message msg = new Message.Builder().addData("message", message).build();
Result result = sender.send(msg, deviceInfo.getDeviceRegistrationID(),
5);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
endpoint.removeDeviceInfo(deviceInfo.getDeviceRegistrationID());
deviceInfo.setDeviceRegistrationID(canonicalRegId);
endpoint.insertDeviceInfo(null, deviceInfo);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
endpoint.removeDeviceInfo(deviceInfo.getDeviceRegistrationID());
}
}
return result;
}
示例14: sendMessage
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
/**
* Sends a message to Android devices.
*
* @param devices the list of devices that the notification is sent to.
* @param message the message to be sent.
* @param playerKey the key of the player that the notification is sent to.
* @throws IOException if there was IOException thrown by Google Cloud Messaging for Android.
*/
protected void sendMessage(List<DeviceEntity> devices, Message message, Key playerKey)
throws IOException {
List<String> androidDeviceIds = new ArrayList<String>();
for (DeviceEntity device : devices) {
androidDeviceIds.add(device.getDeviceId());
}
Sender messageSender = new Sender(Configuration.CLOUD_MESSAGING_API_KEY);
MulticastResult messageResults =
messageSender.send(message, androidDeviceIds, NUMBER_OF_RETRIES);
if (messageResults != null) {
DeviceService deviceService = new DeviceService();
for (int i = 0; i < messageResults.getTotal(); i++) {
Result result = messageResults.getResults().get(i);
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
deviceService.updateDeviceRegistration(devices.get(i), canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// The user has uninstalled the application or turned off notifications.
// Remove the device from Griddler.
deviceService.unregisterDevice(playerKey, devices.get(i).getKey());
} else {
logger.log(Level.INFO, "Error when sending Android push notification: " + error);
}
}
}
}
}
開發者ID:GoogleCloudPlatform,項目名稱:solutions-griddler-sample-backend-java,代碼行數:44,代碼來源:AndroidNotificationService.java
示例15: push
import com.google.android.gcm.server.Result; //導入方法依賴的package包/類
@Override
@ApiMethod(path = "pushToGcm")
public void push(@Named("subscribeId") String subscribeId,
@Named("messageType") String messageType, @Named("message") String message) {
Sender sender = new Sender(API_KEY);
Message msg = new Message.Builder().addData(messageType, message).timeToLive(0).build();
Result result = null;
try {
result = sender.sendNoRetry(msg, subscribeId);
} catch (IOException e) {
log.log(Level.SEVERE, "Error when send message to Google Cloud Messaging Service", e);
return;
}
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
DeviceInfo deviceInfo = endpoint.getDeviceInfo(subscribeId);
endpoint.removeDeviceInfo(subscribeId);
deviceInfo.setId(canonicalRegId);
endpoint.insertDeviceInfo(deviceInfo);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
endpoint.removeDeviceInfo(subscribeId);
}
}
}