1
1
import 'dart:async' ;
2
2
3
- import './services/local.dart' ;
4
- import './services/remote.dart' ;
3
+ import '../../../env.dart' ;
5
4
import '../models/notification.dart' ;
5
+ import 'services/local.dart' ;
6
+ import 'services/remote.dart' ;
6
7
7
8
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;
10
14
11
15
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
+ }
14
38
}
15
39
16
40
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
+ }
19
55
}
20
56
21
57
static Future <void > askPermission () async {
@@ -24,28 +60,78 @@ abstract class PushNotifications {
24
60
// Two prompts will be shown to the user in case when user neither accepts nor rejects notification permission.
25
61
26
62
// 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
+ }
28
77
}
29
78
30
79
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
+ }
33
94
}
34
95
35
96
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
+ }
38
111
}
39
112
40
113
static Future <void > push ({
41
114
required PushNotification notification,
42
115
String ? channel,
43
116
}) async {
44
117
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 ;
48
135
}
49
- await RemoteNotifications .push (notification: notification, channel: channel);
50
136
}
51
137
}
0 commit comments