Skip to content

Commit 48cac15

Browse files
committed
12 Sep 2023
Added user ID to push table - Possible to send push to specific users only.
1 parent 1923b74 commit 48cac15

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

web push/lib/LIB-Push.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
class Push extends Core {
33
// (A) SAVE SUBSCRIBER
44
function save ($endpoint, $sub) {
5-
$this->DB->replace("webpush", ["endpoint", "data"], [$endpoint, $sub]);
5+
$this->DB->replace("webpush",
6+
["endpoint", "user_id", "data"],
7+
[
8+
$endpoint,
9+
isset($_SESSION["user"]) ? $_SESSION["user"]["user_id"] : null,
10+
$sub
11+
]
12+
);
613
return true;
714
}
815

@@ -12,8 +19,8 @@ function del ($endpoint) {
1219
return true;
1320
}
1421

15-
// (C) SEND PUSH
16-
function send ($title, $body, $icon=null, $image=null) {
22+
// (C) SEND PUSH NOTIFICATION
23+
function send ($title, $body, $icon=null, $image=null, $uid=null) {
1724
// (C1) MAY TAKE A LONG TIME IF THERE ARE A LOT OF INACTIVE...
1825
set_time_limit(45);
1926

@@ -25,8 +32,12 @@ function send ($title, $body, $icon=null, $image=null) {
2532
"privateKey" => PUSH_PRIVATE
2633
]]);
2734

28-
// (C3) SEND TO SUBSCRIBERS
29-
$this->DB->query("SELECT `data` FROM `webpush`");
35+
// (C3) SEND TO SUBSCRIBER(S)
36+
$this->DB->query(
37+
"SELECT `data` FROM `webpush`" .
38+
($uid==null ? "" : " WHERE `user_id`=?"),
39+
$uid==null ? null : [$uid]
40+
);
3041
while ($r = $this->DB->stmt->fetchColumn()) {
3142
// (C3-1) SUBSCRIBER
3243
$sub = Minishlink\WebPush\Subscription::create(json_decode($r, true));

web push/lib/SQL-WebPush.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
CREATE TABLE `webpush` (
22
`endpoint` varchar(255) NOT NULL,
3+
`user_id` bigint(20) NULL,
34
`data` text NOT NULL
45
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
56

67
ALTER TABLE `webpush`
7-
ADD PRIMARY KEY (`endpoint`);
8+
ADD PRIMARY KEY (`endpoint`),
9+
ADD KEY `user_id` (`user_id`);

0 commit comments

Comments
 (0)