Skip to content

Commit 3bf6b84

Browse files
committed
bk/2025-05-08-2254
1 parent 0baea57 commit 3bf6b84

9 files changed

Lines changed: 263 additions & 90 deletions

File tree

WebClient/Src/ClientMainView.html

Lines changed: 0 additions & 36 deletions
This file was deleted.

WebClient/Unit1.html

Lines changed: 0 additions & 36 deletions
This file was deleted.

WebClient/WebMainView.dfm

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
object MainView: TMainView
2+
Width = 322
3+
Height = 432
4+
Font.Charset = DEFAULT_CHARSET
5+
Font.Color = clWindowText
6+
Font.Height = -13
7+
Font.Name = 'Tahoma'
8+
Font.Style = []
9+
FormContainer = 'appcontent'
10+
ParentFont = False
11+
OnCreate = WebFormCreate
12+
OnExit = WebFormExit
13+
object WebLabel1: TWebLabel
14+
Left = 8
15+
Top = 11
16+
Width = 43
17+
Height = 16
18+
Caption = 'UserID:'
19+
HeightPercent = 100.000000000000000000
20+
WidthPercent = 100.000000000000000000
21+
end
22+
object WebEdit1: TWebEdit
23+
Left = 57
24+
Top = 8
25+
Width = 216
26+
Height = 19
27+
HeightPercent = 100.000000000000000000
28+
WidthPercent = 100.000000000000000000
29+
OnChange = WebEdit1Change
30+
end
31+
object WebButton1: TWebButton
32+
Left = 8
33+
Top = 33
34+
Width = 265
35+
Height = 25
36+
Caption = 'Subscribe for push notifications'
37+
ChildOrder = 1
38+
HeightPercent = 100.000000000000000000
39+
WidthPercent = 100.000000000000000000
40+
OnClick = WebButton1Click
41+
end
42+
object WebButton2: TWebButton
43+
Left = 8
44+
Top = 64
45+
Width = 265
46+
Height = 25
47+
Caption = 'Unsubscribe from push notifications'
48+
ChildOrder = 2
49+
HeightPercent = 100.000000000000000000
50+
WidthPercent = 100.000000000000000000
51+
OnClick = WebButton2Click
52+
end
53+
object WebPushNotifications1: TWebPushNotifications
54+
RegisterSubscriptionURL = 'http://localhost:8081/registerSubscription'
55+
UnregisterSubscriptionURL = 'http://localhost:8081/unregisterSubscription'
56+
VapidPublickeyURL = 'http://localhost:8081/vapidPublicKey'
57+
Left = 168
58+
Top = 120
59+
end
60+
end

WebClient/WebMainView.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-br" data-bs-theme="dark">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Push Notifications - Delphi</title>
7+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
8+
<style>
9+
body {
10+
background-color: #121212;
11+
color: #f8f9fa;
12+
}
13+
.btn-group button {
14+
font-weight: 500;
15+
transition: all 0.2s;
16+
margin: 30px 0 0 0;
17+
}
18+
.btn-group button:hover {
19+
font-weight: bold;
20+
}
21+
.footer {
22+
height: 60px;
23+
background-color: #1e1e1e;
24+
border-top: 1px solid #343a40;
25+
border-radius: 0 0 12px 12px;
26+
font-size: 14px;
27+
}
28+
</style>
29+
</head>
30+
<body>
31+
<div class="container py-4">
32+
<!-- FORMULARIO -->
33+
<div class="card bg-dark border-secondary rounded-3 p-3 mb-4" id="divFormulario">
34+
<div class="mb-3">
35+
<label for="edtUserID" class="form-label text-white" id="lbUserID">UserID</label>
36+
<input type="text" class="form-control" id="edtUserID" placeholder="Enter Userid">
37+
</div>
38+
39+
<!-- BOTOES -->
40+
<div class="btn-group w-100 mb-3" role="group" id="divBotoes">
41+
<button type="button" class="btn btn-success" id="btnSubscribe">Subscribe for push notifications</button>
42+
<button type="button" class="btn btn-danger" id="btnUnsubscribe">Unsubscribe from push notifications</button>
43+
</div>
44+
</div>
45+
46+
<!-- RODAPE OU ESPACO INFERIOR -->
47+
<div class="footer d-flex align-items-center justify-content-center">
48+
<span class="text-secondary"> 2025 -
49+
<a target="_blank" href="https://www.youtube.com/@code4delphi" class="link-success link-offset-2 link-underline-opacity-0 link-underline-opacity-25-hover">
50+
@Code4Delphi
51+
</a> - Push Notifications - Delphi - TMS Web Core</span>
52+
</div>
53+
</div>
54+
55+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
56+
</body>
57+
</html>

WebClient/WebMainView.pas

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
unit WebMainView;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
7+
WEBLib.StdCtrls, WEBLib.Forms, WEBLib.Dialogs, WEBLib.PushNotifications,
8+
WEBLib.Storage, Vcl.StdCtrls, Vcl.Controls;
9+
10+
type
11+
TMainView = class(TWebForm)
12+
WebEdit1: TWebEdit;
13+
WebButton1: TWebButton;
14+
WebButton2: TWebButton;
15+
WebLabel1: TWebLabel;
16+
WebPushNotifications1: TWebPushNotifications;
17+
procedure WebButton1Click(Sender: TObject);
18+
procedure WebButton2Click(Sender: TObject);
19+
procedure WebEdit1Change(Sender: TObject);
20+
procedure WebFormCreate(Sender: TObject);
21+
procedure WebFormExit(Sender: TObject);
22+
private
23+
ls: TLocalStorage;
24+
procedure DoAddToLocalStorage;
25+
procedure DoRemoveFromLocalStorage;
26+
procedure SetButtonState;
27+
function ValidUserID: Boolean;
28+
end;
29+
30+
var
31+
MainView: TMainView;
32+
33+
implementation
34+
35+
{$R *.dfm}
36+
37+
procedure TMainView.DoAddToLocalStorage;
38+
begin
39+
TWebLocalStorage.SetValue('pushUserID', WebEdit1.Text);
40+
SetButtonState;
41+
end;
42+
43+
procedure TMainView.DoRemoveFromLocalStorage;
44+
begin
45+
TWebLocalStorage.RemoveKey('pushUserID');
46+
SetButtonState;
47+
end;
48+
49+
procedure TMainView.SetButtonState;
50+
begin
51+
if WebEdit1.Text = '' then
52+
begin
53+
WebButton1.Enabled := False;
54+
WebButton2.Enabled := False;
55+
Exit;
56+
end;
57+
58+
if TLocalStorage.GetValue('pushUserID') = WebEdit1.Text then
59+
begin
60+
WebButton1.Enabled := False;
61+
WebButton2.Enabled := True;
62+
end
63+
else if TLocalStorage.GetValue('pushUserID') <> '' then
64+
begin
65+
WebButton1.Enabled := False;
66+
WebButton2.Enabled := False;
67+
end
68+
else
69+
begin
70+
WebButton1.Enabled := True;
71+
WebButton2.Enabled := False;
72+
end;
73+
end;
74+
75+
function TMainView.ValidUserID: Boolean;
76+
begin
77+
Result := True;
78+
if WebEdit1.Text = '' then
79+
begin
80+
Result := False;
81+
ShowMessage('UserID cannot be empty!');
82+
end;
83+
end;
84+
85+
procedure TMainView.WebButton1Click(Sender: TObject);
86+
begin
87+
if ValidUserID then
88+
begin
89+
WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
90+
WebPushNotifications1.RegisterServiceWorker;
91+
92+
DoAddToLocalStorage;
93+
end;
94+
end;
95+
96+
procedure TMainView.WebButton2Click(Sender: TObject);
97+
begin
98+
if ValidUserID then
99+
begin
100+
WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
101+
WebPushNotifications1.Unsubscribe;
102+
103+
DoRemoveFromLocalStorage;
104+
end;
105+
end;
106+
107+
procedure TMainView.WebEdit1Change(Sender: TObject);
108+
begin
109+
SetButtonState;
110+
end;
111+
112+
procedure TMainView.WebFormCreate(Sender: TObject);
113+
begin
114+
ls := TLocalStorage.Create(Self);
115+
if ls.Count > 0 then
116+
begin
117+
WebEdit1.Text := TLocalStorage.GetValue('pushUserID');
118+
end;
119+
120+
SetButtonState;
121+
end;
122+
123+
procedure TMainView.WebFormExit(Sender: TObject);
124+
begin
125+
ls.Free;
126+
end;
127+
128+
end.

WebClient/WebPushClient.dpr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ program WebPushClient;
55
uses
66
Vcl.Forms,
77
WEBLib.Forms,
8-
Unit1 in 'Unit1.pas' {Form2: TWebForm} {*.html};
8+
WebMainView in 'WebMainView.pas' {MainView: TWebForm} {*.html};
99

1010
{$R *.res}
1111

1212
begin
1313
Application.Initialize;
1414
Application.MainFormOnTaskbar := True;
15-
Application.CreateForm(TForm2, Form2);
15+
Application.CreateForm(TMainView, MainView);
1616
Application.Run;
1717
end.

WebClient/WebPushClient.dproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<PropertyGroup Condition="'$(Cfg_1_Win32)'!=''">
9595
<DCC_RemoteDebug>false</DCC_RemoteDebug>
9696
<AppDPIAwarenessMode>PerMonitorV2</AppDPIAwarenessMode>
97+
<TMSWebBrowser>4</TMSWebBrowser>
9798
</PropertyGroup>
9899
<PropertyGroup Condition="'$(Cfg_2)'!=''">
99100
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
@@ -111,9 +112,8 @@
111112
<DelphiCompile Include="$(MainSource)">
112113
<MainSource>MainSource</MainSource>
113114
</DelphiCompile>
114-
<DCCReference Include="Unit1.pas">
115-
<Form>Form2</Form>
116-
<FormType>dfm</FormType>
115+
<DCCReference Include="WebMainView.pas">
116+
<Form>MainView</Form>
117117
<DesignClass>TWebForm</DesignClass>
118118
</DCCReference>
119119
<None Include="index.html"/>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
object ClientMain: TClientMain
1+
object MainView: TMainView
22
Width = 640
33
Height = 480
44
Font.Charset = DEFAULT_CHARSET

0 commit comments

Comments
 (0)