javapush-notificationmessengermatrix-org

Send Matrix message to self with push notification


I'm developing a Java application that sends notifications to its users under certain circumstances.

My idea was to offer Matrix as another notification channel and use the user's own Matrix account to send the messages. (The user has already logged in to a third-party website that also operates a Matrix server. Thanks to SSO, I can use the already stored credentials of the user to log in at the Matrix server.)

While I'm able to send Matrix messages using Cosium/matrix-communication-client, it does not seem that the user receives any push notifications (although the messages are received correctly).

There are two possible scenarios in my view:

  1. The Matrix server doesn't notify the user of messages sent by themselves.
  2. The user's Matrix client (Element, in my test case) ignores notifications about messages sent by the user themselves.

Is that true? Also, can I circumvent this in any way?

Please note that I don't wish to set up a bot account, because that would require me to host a server.

Here's the sample code to create a Matrix room and send a message:

public class Example {
  public static void main(String[] args) {
    MatrixResources matrix =
        MatrixResources.factory()
            .builder()
            .https()
            .hostname("matrix.example.org")
            .defaultPort()
            .usernamePassword("jdoe", "secret")
            .build();

    RoomResource room = matrix
        .rooms()
        .create(
            CreateRoomInput.builder()
                .name("Science")
                .roomAliasName("science")
                .topic("Anything about science")
                .build());
    
    room.sendMessage(Message.builder().body("Hello !").formattedBody("<b>Hello !</b>").build());
  }
}

Solution

  • As pointed out by Johannes Marbach, Element is hard-coded not to show notifications for messages sent by the currently logged-in user. Considering Element is a widely-used app, the idea to send notifications to the user from their own Matrix account cannot be realised universally.

    A work-around is to invite an echo bot to the Matrix room used for notifications. Then the user would, in fact, be notified about the bot's “echo” of the notification message.