0

I´m trying to send a push notification from my EJB class:

@Stateless
public class SendEmailNotificationReminderServiceBean implements SendEmailNotificationReminderService {

    @Inject
    private BeanManager beanManager;

private void sendNotification {
        // Push Nachricht
        PushEvent event = new PushEvent("sendNotification", 1);
        beanManager.fireEvent(event);
}

}

Here my socket:

    @Named
@ApplicationScoped
public class NotificationSocket implements Serializable {

    private static final long serialVersionUID = 7615791377170410627L;

    @Inject
    @Push(channel = "notificationChannel")
    private PushContext push;


    /**
     * Push Notification
     * 
     * @param recipientUser
     */
    public void pushUser(@Observes PushEvent event) {

        if (event == null)
            return;

        Set<Future<Void>> sent = push.send(event.getMessage(), event.getUserId());

    }

}

My object:

public class PushEvent {

    private String message;
    private Long userId;

    public PushEvent(String message) {
        super();
        this.message = message;
    }

    //////////////

    public PushEvent(String message, Long userId) {
        super();
        this.message = message;
        this.userId = userId;
    }

    public String getMessage() {
        return message;
    }

    public Long getUserId() {
        return userId;
    }

}

My JSF page:

<o:socket channel="notificationChannel"
        user="#{loginBean.currentEmployee.id}" scope="session"
        onmessage="notificationLoadScript">
    </o:socket>

    <h:form id="notificationPushTopbarForm">
        <p:remoteCommand name="notificationLoadScript"
            actionListener="#{topbarMenuController.loadNotification()}"
            oncomplete="changeTitleForNotification(#{topbarMenuController.numberOfNewNotificationAvailable})"
            update=":notificationLink, :notificationSidebarForm" global="false" />
    </h:form>

What I´m trying to do is:

  • Create a PushNotification from my backend (EJB layer) to the current logged in User and this channel.
    As example you can imagine the Notification icon (right corner) from Stackoverflow.

I´m getting no error message, but the JSF component is also not updating (p:remoteCommand call). Any idea how I can fix this?

10
  • This will indeed not work. There is no means of a JSF request at all at that point. Why exactly do you want to add these faces messages in the receiving side? You should basically be adding them in the sending side.
    – BalusC
    Mar 30, 2021 at 18:14
  • There is not 100% a need to add the Faces Message. My main goal is, that the logged in user will get a refresh (update) on a specific element from the page (=> call the p:remoteCommand name="notificationLoadScript")... The "public void pushUser(@Observes PushEvent event)" is the sending side? Maybe a short background: I have a scheduler in my backend. This scheduler execute actions, like create a "Reminder Object" in the backend. Furthermore it will create a Push Notification (as you know from Stackoverflow e.g.)...
    – vished2000
    Mar 30, 2021 at 18:23
  • Nope, it's the receiving side (of the push event, that is). Just get rid of attempts to add faces messages there. These will never work. FacesContext#getCurrentInstance() will always return null there. Simply because it's not initiated by a JSF command button or so.
    – BalusC
    Mar 30, 2021 at 18:24
  • Please Ctrl+F "The FacesContext will also be unavailable in the above event listener method." in o:socket documentation. That's already your answer. The bean/method is not invoked by a HTTP/JSF request at all. It's invoked internally by a EJB/CDI event.
    – BalusC
    Mar 30, 2021 at 18:26
  • Ok, thanks... Any idea how I can then call then p:remoteCommand after I´ve send a push event?
    – vished2000
    Mar 30, 2021 at 18:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.