use of com.willshex.blogwt.shared.api.datatype.PushToken in project blogwt by billy1380.
the class SetPushTokenActionHandler method handle.
@Override
public void handle(SetPushTokenRequest input, SetPushTokenResponse output) throws Exception {
ApiValidator.request(input, SetPushTokenRequest.class);
ApiValidator.accessCode(input.accessCode, "input.accessCode");
output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
if (input.token != null && input.token.user == null) {
input.token.user = input.session.user;
}
input.token = PushTokenValidator.valiate(input.token, "input.token");
PushToken token = PushTokenServiceProvider.provide().getUserPlatformPushToken(input.token.user, input.token.platform);
if (token == null) {
input.token = PushTokenServiceProvider.provide().addPushToken(input.token);
} else {
if (input.token.value != null) {
token.value = input.token.value;
}
PushTokenServiceProvider.provide().updatePushToken(token);
}
}
use of com.willshex.blogwt.shared.api.datatype.PushToken in project blogwt by billy1380.
the class SetPushTokenRequest method fromJson.
@Override
public void fromJson(JsonObject jsonObject) {
super.fromJson(jsonObject);
if (jsonObject.has("token")) {
JsonElement jsonToken = jsonObject.get("token");
if (jsonToken != null) {
token = new PushToken();
token.fromJson(jsonToken.getAsJsonObject());
}
}
}
use of com.willshex.blogwt.shared.api.datatype.PushToken in project blogwt by billy1380.
the class NotificationHelper method sendNotification.
public static List<Notification> sendNotification(String code, User user, final Map<String, ?> values) {
List<Notification> notifications = new ArrayList<>();
final MetaNotification meta = MetaNotificationServiceProvider.provide().getCodeMetaNotification(code);
if (meta == null) {
if (LOG.isLoggable(Level.WARNING)) {
LOG.warning("No meta notification found for code [" + code + "]");
}
} else {
NotificationSetting setting = NotificationSettingServiceProvider.provide().getMetaUserNotificationSetting(meta, user);
if (setting == null) {
setting = NotificationSettingServiceProvider.provide().addNotificationSetting(new NotificationSetting().meta(meta).selected(meta.defaults).user(user));
}
String content;
final Notification template = new Notification().content(content = InflatorHelper.inflate(values, meta.content)).meta(meta).target(user);
Notification notification;
String title = "Message from " + PropertyHelper.value(PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.TITLE));
if (setting.selected == null || setting.selected.size() == 0) {
if (LOG.isLoggable(Level.INFO)) {
LOG.info("No notification modes found for [" + code + "] and user [" + user + "] adding template [" + template + "]");
}
notifications.add(NotificationServiceProvider.provide().addNotification(template));
} else {
for (NotificationModeType mode : setting.selected) {
notification = JsonableHelper.copy(template, new Notification()).mode(mode);
switch(mode) {
case NotificationModeTypeEmail:
EmailHelper.sendEmail(user.email, UserHelper.name(user), title, content, false);
break;
case NotificationModeTypePush:
List<PushToken> tokens = PushTokenServiceProvider.provide().getUserPushTokens(user);
Jsonable data = new Jsonable() {
@Override
public JsonObject toJson() {
JsonObject object = super.toJson();
object.addProperty("code", meta.code);
if (values != null) {
Object value;
DataType slim;
for (String key : values.keySet()) {
value = values.get(key);
if (value instanceof DataType) {
slim = PersistenceHelper.slim(DataType.class, (DataType) value, null);
object.add(key, slim.toJson());
}
}
}
return object;
}
};
for (PushToken pushToken : tokens) {
PushHelper.push(pushToken, meta.name, content, data);
}
if (LOG.isLoggable(Level.FINE) && tokens.size() == 0) {
LOG.fine("Could not push because no tokens found [" + meta.name + "], [" + content + "] payload approximation [" + PushHelper.buildPayload(new PushToken().platform("android").user(user).value("approx-test-token"), new AndroidMessage().title(meta.name).body(content), data) + "]");
}
break;
case NotificationModeTypeSms:
break;
}
notifications.add(NotificationServiceProvider.provide().addNotification(notification));
}
}
}
return notifications;
}
use of com.willshex.blogwt.shared.api.datatype.PushToken in project blogwt by billy1380.
the class PushTokenService method addPushToken.
@Override
public PushToken addPushToken(PushToken pushToken) {
if (pushToken.created == null) {
pushToken.created = new Date();
}
if (pushToken.user != null) {
pushToken.userKey = Key.create(pushToken.user);
}
Key<PushToken> key = provide().save().entity(pushToken).now();
pushToken.id = Long.valueOf(key.getId());
return pushToken;
}
Aggregations