Skip to content

Commit bbeb5b6

Browse files
Added: env variable to control push notification service
1 parent bc0fec6 commit bbeb5b6

File tree

4 files changed

+117
-23
lines changed

4 files changed

+117
-23
lines changed

lib/vaahextendflutter/env.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final EnvironmentConfig defaultConfig = EnvironmentConfig(
2525
enableLocalLogs: true,
2626
enableCloudLogs: true,
2727
enableApiLogInterceptor: true,
28+
pushNotificationsServiceType: PushNotificationsServiceType.none,
2829
internalNotificationsServiceType: InternalNotificationsServiceType.none,
2930
showDebugPanel: true,
3031
debugPanelColor: AppTheme.colors['black']!.withOpacity(0.8),
@@ -96,6 +97,7 @@ class EnvironmentConfig {
9697
final bool enableCloudLogs;
9798
final SentryConfig? sentryConfig;
9899
final bool enableApiLogInterceptor;
100+
final PushNotificationsServiceType pushNotificationsServiceType;
99101
final InternalNotificationsServiceType internalNotificationsServiceType;
100102
final OneSignalConfig? oneSignalConfig;
101103
final PusherConfig? pusherConfig;
@@ -117,6 +119,7 @@ class EnvironmentConfig {
117119
required this.enableCloudLogs,
118120
this.sentryConfig,
119121
required this.enableApiLogInterceptor,
122+
required this.pushNotificationsServiceType,
120123
required this.internalNotificationsServiceType,
121124
this.oneSignalConfig,
122125
this.pusherConfig,
@@ -161,6 +164,7 @@ class EnvironmentConfig {
161164
bool? enableCloudLogs,
162165
SentryConfig? sentryConfig,
163166
bool? enableApiLogInterceptor,
167+
PushNotificationsServiceType? pushNotificationsServiceType,
164168
InternalNotificationsServiceType? internalNotificationsServiceType,
165169
OneSignalConfig? oneSignalConfig,
166170
PusherConfig? pusherConfig,
@@ -182,6 +186,8 @@ class EnvironmentConfig {
182186
enableCloudLogs: enableCloudLogs ?? this.enableCloudLogs,
183187
sentryConfig: sentryConfig ?? this.sentryConfig,
184188
enableApiLogInterceptor: enableApiLogInterceptor ?? this.enableApiLogInterceptor,
189+
pushNotificationsServiceType:
190+
pushNotificationsServiceType ?? this.pushNotificationsServiceType,
185191
internalNotificationsServiceType:
186192
internalNotificationsServiceType ?? this.internalNotificationsServiceType,
187193
oneSignalConfig: oneSignalConfig ?? this.oneSignalConfig,
@@ -198,6 +204,10 @@ class EnvironmentConfig {
198204
}
199205
}
200206

207+
enum PushNotificationsServiceType { local, remote, both, none }
208+
209+
enum InternalNotificationsServiceType { pusher, firebase, custom, none }
210+
201211
class SentryConfig {
202212
final String dsn;
203213
final bool autoAppStart; // To record cold and warm start up time
@@ -252,8 +262,6 @@ class OneSignalConfig {
252262
}
253263
}
254264

255-
enum InternalNotificationsServiceType { pusher, firebase, custom, none }
256-
257265
class PusherConfig {
258266
final String apiKey;
259267
final String cluster;
Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
11
import 'dart:async';
22

3-
import './services/local.dart';
4-
import './services/remote.dart';
3+
import '../../../env.dart';
54
import '../models/notification.dart';
5+
import 'services/local.dart';
6+
import 'services/remote.dart';
67

78
abstract class PushNotifications {
8-
static final Stream<String> remoteUserIdStream = RemoteNotifications.userIdStream;
9-
static String? get remoteUserId => RemoteNotifications.userId;
9+
static final PushNotificationsServiceType _pushNotificationsServiceType =
10+
EnvironmentConfig.getEnvConfig().pushNotificationsServiceType;
11+
12+
static late final Stream<String> userIdStream;
13+
static late String? userId;
1014

1115
static Future<void> init() async {
12-
await RemoteNotifications.init();
13-
await LocalNotifications.init();
16+
switch (_pushNotificationsServiceType) {
17+
case PushNotificationsServiceType.local:
18+
await LocalNotifications.init();
19+
userIdStream = const Stream.empty();
20+
userId = null;
21+
return;
22+
case PushNotificationsServiceType.remote:
23+
await RemoteNotifications.init();
24+
userIdStream = RemoteNotifications.userIdStream;
25+
userId = RemoteNotifications.userId;
26+
return;
27+
case PushNotificationsServiceType.both:
28+
await RemoteNotifications.init();
29+
await LocalNotifications.init();
30+
userIdStream = RemoteNotifications.userIdStream;
31+
userId = RemoteNotifications.userId;
32+
return;
33+
case PushNotificationsServiceType.none:
34+
userIdStream = const Stream.empty();
35+
userId = null;
36+
return;
37+
}
1438
}
1539

1640
static void dispose() {
17-
RemoteNotifications.dispose();
18-
LocalNotifications.dispose();
41+
switch (_pushNotificationsServiceType) {
42+
case PushNotificationsServiceType.local:
43+
LocalNotifications.dispose();
44+
return;
45+
case PushNotificationsServiceType.remote:
46+
RemoteNotifications.dispose();
47+
return;
48+
case PushNotificationsServiceType.both:
49+
RemoteNotifications.dispose();
50+
LocalNotifications.dispose();
51+
return;
52+
case PushNotificationsServiceType.none:
53+
return;
54+
}
1955
}
2056

2157
static Future<void> askPermission() async {
@@ -24,28 +60,78 @@ abstract class PushNotifications {
2460
// Two prompts will be shown to the user in case when user neither accepts nor rejects notification permission.
2561

2662
// Local notification permission always works even if project has no setup for onesignal
27-
await LocalNotifications.askPermission();
63+
64+
switch (_pushNotificationsServiceType) {
65+
case PushNotificationsServiceType.local:
66+
await LocalNotifications.askPermission();
67+
return;
68+
case PushNotificationsServiceType.remote:
69+
await RemoteNotifications.askPermission();
70+
return;
71+
case PushNotificationsServiceType.both:
72+
await LocalNotifications.askPermission();
73+
return;
74+
case PushNotificationsServiceType.none:
75+
return;
76+
}
2877
}
2978

3079
static Future<void> subscribe() async {
31-
await RemoteNotifications.subscribe();
32-
await LocalNotifications.subscribe();
80+
switch (_pushNotificationsServiceType) {
81+
case PushNotificationsServiceType.local:
82+
await LocalNotifications.subscribe();
83+
return;
84+
case PushNotificationsServiceType.remote:
85+
await RemoteNotifications.subscribe();
86+
return;
87+
case PushNotificationsServiceType.both:
88+
await RemoteNotifications.subscribe();
89+
await LocalNotifications.subscribe();
90+
return;
91+
case PushNotificationsServiceType.none:
92+
return;
93+
}
3394
}
3495

3596
static Future<void> unsubscribe() async {
36-
await RemoteNotifications.unsubscribe();
37-
await LocalNotifications.unsubscribe();
97+
switch (_pushNotificationsServiceType) {
98+
case PushNotificationsServiceType.local:
99+
await LocalNotifications.unsubscribe();
100+
return;
101+
case PushNotificationsServiceType.remote:
102+
await RemoteNotifications.unsubscribe();
103+
return;
104+
case PushNotificationsServiceType.both:
105+
await RemoteNotifications.unsubscribe();
106+
await LocalNotifications.unsubscribe();
107+
return;
108+
case PushNotificationsServiceType.none:
109+
return;
110+
}
38111
}
39112

40113
static Future<void> push({
41114
required PushNotification notification,
42115
String? channel,
43116
}) async {
44117
assert(notification.content.trim().isNotEmpty);
45-
if (NotificationType.local == notification.type) {
46-
await LocalNotifications.push(notification: notification);
47-
return;
118+
119+
switch (_pushNotificationsServiceType) {
120+
case PushNotificationsServiceType.local:
121+
await LocalNotifications.push(notification: notification);
122+
return;
123+
case PushNotificationsServiceType.remote:
124+
await RemoteNotifications.push(notification: notification, channel: channel);
125+
return;
126+
case PushNotificationsServiceType.both:
127+
if (NotificationType.local == notification.type) {
128+
await LocalNotifications.push(notification: notification);
129+
return;
130+
}
131+
await RemoteNotifications.push(notification: notification, channel: channel);
132+
return;
133+
case PushNotificationsServiceType.none:
134+
return;
48135
}
49-
await RemoteNotifications.push(notification: notification, channel: channel);
50136
}
51137
}

lib/vaahextendflutter/widgets/debug.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ class _NotificationSection extends StatefulWidget {
503503
}
504504

505505
class __NotificationSectionState extends State<_NotificationSection> {
506-
String? userId = PushNotifications.remoteUserId;
506+
String? userId = PushNotifications.userId;
507507

508508
@override
509509
void initState() {
510510
super.initState();
511-
PushNotifications.remoteUserIdStream.listen((String updatedUserId) {
511+
PushNotifications.userIdStream.listen((String updatedUserId) {
512512
setState(() {
513513
userId = updatedUserId;
514514
});

lib/views/pages/home.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:flutter/material.dart';
2-
import 'package:vaahflutter/vaahextendflutter/services/notification/internal/notification_view.dart';
32

4-
import './ui/index.dart';
53
import '../../vaahextendflutter/base/base_stateful.dart';
64
import '../../vaahextendflutter/env.dart';
5+
import '../../vaahextendflutter/services/notification/internal/notification_view.dart';
6+
import 'ui/index.dart';
77

88
class HomePage extends StatefulWidget {
99
static const String routePath = '/home';

0 commit comments

Comments
 (0)