Skip to content

Commit

Permalink
fix: add activity/comment link for push notification - EXO-67587 - Me…
Browse files Browse the repository at this point in the history
…eds-io/meeds#1299



Push notifications require that we put the notification link in the subject property to be used later as the link to redirect to when a user clicks on the received notification on their phones.
This fix makes sure that the link is added ion each notification otherwise, the curret server URL (Home) will be opened.
  • Loading branch information
ahamdi authored Nov 20, 2023
1 parent 747c24a commit f63f6d4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.exoplatform.push.channel;

import org.apache.commons.lang.StringUtils;
import org.exoplatform.commons.api.notification.NotificationContext;
import org.exoplatform.commons.api.notification.channel.AbstractChannel;
import org.exoplatform.commons.api.notification.channel.template.AbstractTemplateBuilder;
Expand Down Expand Up @@ -94,6 +95,11 @@ public void dispatch(NotificationContext ctx, String userId) {
String maskedToken = StringUtil.mask(device.getToken(), 4);
LOG.info("Sending push notification to user {} (token={})",
userId, maskedToken);
if(StringUtils.isBlank(messageInfo.getSubject())) {
if(LOG.isDebugEnabled()) {
LOG.warn("No URL provided for notification type {}", messageInfo.getPluginId());
}
}
Message message = new Message(userId, device.getToken(), device.getType(), brandingService.getCompanyName(), messageInfo.getBody(), messageInfo.getSubject());
messagePublisher.send(message);
long sendMessageExecutionTime = System.currentTimeMillis() - startTimeSendingMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
}
//
boolean notHighLightComment = Boolean.parseBoolean(notification.getValueOwnerParameter(NotificationMessageUtils.NOT_HIGHLIGHT_COMMENT_PORPERTY.getKey()));
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(notHighLightComment ? activity.getId() : activity.getId() + "#comment-" + commentActivity.getId()));
String notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(notHighLightComment ? activity.getId() : activity.getId() + "#comment-" + commentActivity.getId());
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink);

//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(notificationLink).end();
}

private String cutStringByMaxLength(String st, int maxLength) {
Expand Down Expand Up @@ -346,14 +347,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
}
//

templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId() + "#comment-" + replyToCommentActivity.getId()));
String notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId() + "#comment-" + replyToCommentActivity.getId());
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink);

//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(notificationLink).end();
}

private String cutStringByMaxLength(String st, int maxLength) {
Expand Down Expand Up @@ -401,21 +403,23 @@ protected MessageInfo makeMessage(NotificationContext ctx) {

// In case of mention on a comment, we need provide the id of the activity, not of the comment
String activityTitle = getActivityTitle(activity);
String activityLink;
if (activity.isComment()) {
ExoSocialActivity parentActivity = Utils.getActivityManager().getParentActivity(activity);
activityTitle = getActivityTitle(parentActivity);
activityId = parentActivity.getId();
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityId + "#comment-" + activity.getId()));
activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityId + "#comment-" + activity.getId());
} else {
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityId));
activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityId);
}
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink);
templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(activityTitle, activity.getType()));
//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(activityLink).end();
}

@Override
Expand Down Expand Up @@ -462,25 +466,28 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(getActivityTitle(activity), activity.getType()));
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId()));

String notificationLink;
if(activity.isComment()) {
ExoSocialActivity activityOfComment = Utils.getActivityManager().getParentActivity(activity);
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityOfComment.getId() + "#comment-" + activity.getId()));
notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityOfComment.getId() + "#comment-" + activity.getId());

} else {
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId()));
notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId());
}
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink);
List<String> users = SocialNotificationUtils.mergeUsers(notification, SocialNotificationUtils.LIKER.getKey(), activity.getId(), notification.getValueOwnerParameter(SocialNotificationUtils.LIKER.getKey()));
//
int nbUsers = users.size();
if (nbUsers > 0) {
Identity lastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 1), true);
Identity lastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 1));
Profile profile = lastIdentity.getProfile();
templateContext.put("USER", Utils.addExternalFlag(lastIdentity));
templateContext.put("AVATAR", profile.getAvatarUrl() != null ? profile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL);
templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(lastIdentity.getRemoteId()));
templateContext.put("NB_USERS", nbUsers);
//
if (nbUsers >= 2) {
Identity beforeLastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 2), true);
Identity beforeLastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 2));
templateContext.put("LAST_USER", Utils.addExternalFlag(beforeLastIdentity));
if (nbUsers > 2) {
templateContext.put("COUNT", nbUsers - 2);
Expand All @@ -493,7 +500,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(notificationLink).end();
}

@Override
Expand Down Expand Up @@ -523,14 +530,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR));
templateContext.put("USER", Utils.addExternalFlag(identity));
templateContext.put("PORTAL_NAME", NotificationPluginUtils.getBrandingPortalName());
templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId()));
String profileUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getUserProfileUri(identity.getRemoteId());
templateContext.put("PROFILE_URL", profileUrl);
templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL);
//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(profileUrl).end();
}

@Override
Expand Down Expand Up @@ -571,13 +579,14 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(getActivityTitle(activity), activity.getType()));
templateContext.put("USER", Utils.addExternalFlag(identity));
templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId()));
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId()));
String activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId());
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink) ;
//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(activityLink).end();
}

@Override
Expand Down Expand Up @@ -625,13 +634,14 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("SPACE", space.getDisplayName());
templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", "")));
templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId()));
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId()));
String activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId());
templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink);
//
String body = TemplateUtils.processGroovy(templateContext);
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(activityLink).end();
}

@Override
Expand Down Expand Up @@ -663,7 +673,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("STATUS", status != null && status.equals("accepted") ? "ACCEPTED" : "PENDING");
templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR));
templateContext.put("USER", Utils.addExternalFlag(identity));
templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId()));
String senderProfileUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getUserProfileUri(identity.getRemoteId());
templateContext.put("PROFILE_URL", senderProfileUrl);
templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL);
templateContext.put("ACCEPT_CONNECTION_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(ACCEPT_INVITATION_TO_CONNECT, sender, toUser, notification.getId(), MESSAGE_JSON_FILE_NAME));
templateContext.put("REFUSE_CONNECTION_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_INVITATION_TO_CONNECT, sender, toUser, notification.getId(), MESSAGE_JSON_FILE_NAME));
Expand All @@ -672,7 +683,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(senderProfileUrl).end();
}

@Override
Expand Down Expand Up @@ -710,7 +721,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR));
templateContext.put("SPACE", space.getDisplayName());
templateContext.put("USER", Utils.addExternalFlag(identity));
templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", "")));
String spaceUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", ""));
templateContext.put("SPACE_URL", spaceUrl);
templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL);
templateContext.put("VALIDATE_SPACE_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(VALIDATE_SPACE_REQUEST, space.getId(), identity.getRemoteId()) + "/" + notification.getTo() + "/" + notification.getId() + "/" + MESSAGE_JSON_FILE_NAME);
templateContext.put("REFUSE_SPACE_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_SPACE_REQUEST, space.getId(), identity.getRemoteId()) + "/" + notification.getTo() + "/" + notification.getId() + "/" + MESSAGE_JSON_FILE_NAME);
Expand All @@ -719,7 +731,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(spaceUrl).end();
}

@Override
Expand Down Expand Up @@ -756,7 +768,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR));
templateContext.put("SPACE", space.getDisplayName());
templateContext.put("SENDER_NAME", sender);
templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", "")));
String spaceUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", ""));
templateContext.put("SPACE_URL", spaceUrl);
templateContext.put("SPACE_AVATAR", space.getAvatarUrl() != null ? space.getAvatarUrl() : LinkProvider.SPACE_DEFAULT_AVATAR_URL);
templateContext.put("ACCEPT_SPACE_INVITATION_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(ACCEPT_SPACE_INVITATION, space.getId(), notification.getTo(), notification.getId(), MESSAGE_JSON_FILE_NAME));
templateContext.put("REFUSE_SPACE_INVITATION_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_SPACE_INVITATION, space.getId(), notification.getTo(), notification.getId(), MESSAGE_JSON_FILE_NAME));
Expand All @@ -765,7 +778,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) {
//binding the exception throws by processing template
ctx.setException(templateContext.getException());
MessageInfo messageInfo = new MessageInfo();
return messageInfo.body(body).end();
return messageInfo.body(body).subject(spaceUrl).end();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.exoplatform.commons.api.notification.plugin.NotificationPluginUtils;
import org.exoplatform.commons.api.notification.service.WebNotificationService;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.push.domain.Message;
Expand Down Expand Up @@ -147,6 +148,9 @@ public void send(Message message) throws Exception {
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");

String messageBody = processBody(message);
if(StringUtils.isEmpty(message.getUrl())) {
message.setUrl(CommonsUtils.getCurrentDomain());
}

StringBuilder requestBody = new StringBuilder()
.append("{")
Expand Down

0 comments on commit f63f6d4

Please sign in to comment.