home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Push Notifications (Websocket only) can not be handled after the first time

tonka3000 opened this issue · comments

Hi,

I try to implement Push Notifications (Websocket only) but I hit a big problem. I follow the guide from https://developers.home-assistant.io/docs/api/native-app-integration/notifications and messages are deployed as expected. This only works for the first time after sending ...

con.sendMessage({
      type: "mobile_app/push_notification_channel",
      webhook_id: webhook_id,
      support_confirm: false
    });

Current Steps

  1. Send mobile_app/push_notification_channel
  2. Use HA DevTools to send a notification via notify.mobile_app_<mydevice> -> Works
  3. Use HA DevTools to send a second notification via notify.mobile_app_<mydevice> -> Fail

Cause

I checked the source code of 8.1.0 and saw that the library only accept events from HA when they were send from the client first (checked via the id). The problem is that in the Push Notification way this is not the case because HA is the initator.
The current source of this lib force an unsubscribing from the current websocket when the command was not send from the client first which let the second try fail.

How can I handle that case with this library?

Thanks in advance

You shouldn't send a message, you should use con.subscribeMessage.

@balloob thanks, I will try it.

In the case somebody has the same question and find this issue.

I got it to works with the following snippet.

await con.subscribeMessage(
          (result) => {
            console.log(result);
          },
          {
            type: "mobile_app/push_notification_channel",
            webhook_id: webhook_id,
            support_confirm: false,
          },
          { resubscribe: true }
);