in case anyone else is struggling with this I managed to make some progress, my stack is ionic 5 and angular 8, first I created a component in charge of loading Tawk to my PWA, with this simple code
import {Component, Inject, Input, OnDestroy, OnInit, Renderer2} from '@angular/core';
import {DOCUMENT} from '@angular/common';
@Component({
selector: 'app-tawk-chat',
templateUrl: './tawk-chat.component.html',
styleUrls: ['./tawk-chat.component.scss'],
})
export class TawkChatComponent implements OnInit {
@Input() id: string;
script = this._renderer.createElement('script');
constructor(private _renderer: Renderer2, @Inject(DOCUMENT) private _document, ) {}
ngOnInit() {
this.script.text = `var Tawk_API = Tawk_API || {}, Tawk_LoadStart = new Date();
(function () {
var s1 = document.createElement("script"), s0 = document.getElementsByTagName("script")[0];
s1.async = true;
s1.src = 'https://embed.tawk.to/${this.id}/default';
s1.charset = 'UTF-8';
s1.setAttribute('crossorigin', '*');
s0.parentNode.insertBefore(s1, s0);
})();`;
this._renderer.appendChild(this._document.body, this.script);
}
}
The referenced the component on my app.component.html like this
<ion-app>
<ion-router-outlet id="content1"></ion-router-outlet>
<app-tawk-chat [id]="_enviroment.tawkId"></app-tawk-chat>
</ion-app>
Then found out from here https://www.tawk.to/knowledgebase/advanced-features/using-a-text-link-or-button-to-open-the-chat-widget/ that you can set the widget as hidden by default.
Finally, just call this function whenever you want to open the chat
openChat() {
window.Tawk_API.maximize();
}
Also, you can use this._window.Tawk_API to access any of the method defined on https://www.tawk.to/javascript-api/#onChatStarted
Only limitation that I'm still facing, is the following
Client opens the chat and sends a message
Operator receives the message and is typing a response
Client closes the chat before receiving response
operator sends the response
WebApp makes a soft click noise but no notification.
I guess I'll need to make some sort of workaround through push notification, so when an operator is chatting with someone and the person closes the chat, a notification is sent "manually"