forked from humhub/fcm-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.php
127 lines (98 loc) · 4.06 KB
/
Events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace humhub\modules\fcmPush;
use humhub\modules\fcmPush\assets\FcmPushAsset;
use humhub\modules\fcmPush\assets\FirebaseAsset;
use humhub\modules\fcmPush\components\NotificationTargetProvider;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\services\DriverService;
use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget;
use humhub\modules\notification\targets\MobileTargetProvider;
use humhub\modules\web\pwa\controllers\ManifestController;
use humhub\modules\web\pwa\controllers\ServiceWorkerController;
use humhub\widgets\BaseStack;
use Yii;
class Events
{
private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout';
private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin';
public static function onBeforeRequest($event)
{
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if ($module->getDriverService()->hasConfiguredDriver()) {
Yii::$container->set(MobileTargetProvider::class, NotificationTargetProvider::class);
}
}
public static function onManifestControllerInit($event)
{
/** @var ManifestController $controller */
$controller = $event->sender;
$controller->manifest['gcm_sender_id'] = (string)103953800507;
}
public static function onServiceWorkerControllerInit($event): void
{
/** @var ServiceWorkerController $controller */
$controller = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if (!$module->getDriverService()->hasConfiguredWebDriver()) {
return;
}
$bundle = FirebaseAsset::register(Yii::$app->view);
$pushDriver = (new DriverService($module->getConfigureForm()))->getWebDriver();
// Service Worker Addons
$controller->additionalJs .= <<<JS
// Give the service worker access to Firebase Messaging.
importScripts('{$bundle->baseUrl}/firebase-app.js');
importScripts('{$bundle->baseUrl}/firebase-messaging.js');
//importScripts('https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js');
//importScripts('https://www.gstatic.com/firebasejs/6.3.3/firebase-messaging.js');
firebase.initializeApp({messagingSenderId: "{$pushDriver->getSenderId()}"});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: payload.data.icon
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
JS;
}
public static function onNotificationInfoWidget($event)
{
/** @var BaseStack $baseStack */
$baseStack = $event->sender;
$baseStack->addWidget(PushNotificationInfoWidget::class);
}
public static function onLayoutAddonInit($event)
{
if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) {
MobileAppHelper::registerLogoutScript();
Yii::$app->session->remove(self::SESSION_VAR_LOGOUT);
}
if (Yii::$app->session->has(self::SESSION_VAR_LOGIN)) {
MobileAppHelper::registerLoginScript();
MobileAppHelper::registerNotificationScript();
Yii::$app->session->remove(self::SESSION_VAR_LOGIN);
}
if (Yii::$app->user->isGuest) {
return;
}
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if (!$module->getDriverService()->hasConfiguredWebDriver()) {
return;
}
FcmPushAsset::register(Yii::$app->view);
FirebaseAsset::register(Yii::$app->view);
}
public static function onAfterLogout()
{
Yii::$app->session->set(self::SESSION_VAR_LOGOUT, 1);
}
public static function onAfterLogin()
{
Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1);
}
}