cancel
Showing results for 
Search instead for 
Did you mean: 

implement an operation - Subscribe to an Alert

mcaissie_
Confirmed Champ
Confirmed Champ

Hi

I would like to implement an operation to allow me to subscribe a user to an alert. So i am looking for the same behavior than if a go in Document Management - click on a file

  • select Alerts and click on Subscribe Modification.

In Java Doc i found the NotificationManager with the method; addSubscription(String username, String notification, DocumentModel doc, Boolean sendConfirmationEmail, NuxeoPrincipal principal, String notificationName)

But i am not sure what the two Strings "notification" and "notificationName" represents and how they are linked with the actual DocumentEventTypes.

I tried the following with no success;

@Param(name = "username")
protected String username;

@Param(name = "DocumentEventType")
protected String DocumentEventType;

@OperationMethod
public  run(DocumentModel doc) throws Exception{

NotificationManager  notificationManager = Framework.getService(NotificationManager.class);
				
notificationManager.addSubscription(username, DocumentEventType, doc, true, null, DocumentEventType);

If i get;

notificationManager.getSubscriptionsForUserOnDocument(username, doc.getId())

i can see a notification "documentModified" but i don't see it selected if i look in Document Management.

Does anyone have an idea on how to do this? ...examples?... links to source code ?

tx

Michel

1 ACCEPTED ANSWER

Julien_Carsique
Elite Collaborator
Elite Collaborator

"notification" is used in the UserSubscription to identify the notification:

UserSubscription subscription = new UserSubscription(notification, username, doc.getId());

whereas "notificationName" is only a label used in the sent email:

// options for confirmation email
options.put("recipients", username);
options.put("notifName", notificationName);

View answer in original post

4 REPLIES 4

Alain_ESCAFFRE
Star Contributor
Star Contributor

Hi,

That's a nice approach:) First of all, for the username, you must prefix it for telling if it is a group or a user. The prefix to use are:

  • NotificationConstants.USER_PREFIX
  • NotificationConstants.GROUP_PREFIX

Then, I can see that the API expects the principal (you put null). You should probably put the principal of the current Session.

Julien_Carsique
Elite Collaborator
Elite Collaborator

"notification" is used in the UserSubscription to identify the notification:

UserSubscription subscription = new UserSubscription(notification, username, doc.getId());

whereas "notificationName" is only a label used in the sent email:

// options for confirmation email
options.put("recipients", username);
options.put("notifName", notificationName);

hi Julient

mcaissie_
Confirmed Champ
Confirmed Champ

Hi,

Finally the USER_PREFIX constant resolved the problem. Concerning the principal it's not mandatory, but if you put it , the user will also receive a notification indicating that the Principal (Administrator in this context) subscribed him to the notification.

I didn't understand what the UserSubscription subscription = new UserSubscription(notification, username, doc.getId()); add to this. I can subscribe to a notification , receive the email , without it.

@Param(name = "username") protected String username;

@OperationMethod
public String run(DocumentModel doc) throws Exception{
	
	final String CONSTANT_CREATION = "Creation";
	
	try
	{
				
	//Create a notification Manager	
	NotificationManager  notificationManager = Framework.getService(NotificationManager.class);
	
	//add a subscription
	notificationManager.addSubscription(NotificationConstants.USER_PREFIX + username,CONSTANT_CREATION, doc, true, (NuxeoPrincipal)doc.getCoreSession().getPrincipal(), null);
	
	//save session
	session.save();