-
Notifications
You must be signed in to change notification settings - Fork 16
/
push_notification_service.dart
265 lines (229 loc) · 9.13 KB
/
push_notification_service.dart
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import 'dart:async';
import 'dart:io';
import 'package:ably_flutter/ably_flutter.dart' as ably;
import 'package:ably_flutter_example/constants.dart';
import 'package:ably_flutter_example/push_notifications/android_push_notification_configuration.dart';
import 'package:ably_flutter_example/push_notifications/push_notification_message_examples.dart';
import 'package:rxdart/rxdart.dart';
class PushNotificationService {
final bool useRealtimeClient;
final ably.Realtime _realtime;
final ably.Rest _rest;
late ably.RealtimeChannel? _realtimeChannel;
late ably.RealtimeChannel? _pushLogMetaChannel;
late ably.RestChannel? _restChannel;
late ably.PushChannel? _pushChannel;
PushNotificationService(
this._realtime,
this._rest, {
this.useRealtimeClient = true,
}) {
_getChannels();
getDevice();
if (Platform.isIOS) {
updateNotificationSettings();
}
AndroidPushNotificationConfiguration();
}
final BehaviorSubject<ably.PaginatedResult<ably.PushChannelSubscription>>
_pushChannelDeviceSubscriptionsSubject =
BehaviorSubject<ably.PaginatedResult<ably.PushChannelSubscription>>();
ValueStream<ably.PaginatedResult<ably.PushChannelSubscription>>
get pushChannelDeviceSubscriptionsStream =>
_pushChannelDeviceSubscriptionsSubject.stream;
final BehaviorSubject<ably.PaginatedResult<ably.PushChannelSubscription>>
_pushChannelClientSubscriptionsSubject =
BehaviorSubject<ably.PaginatedResult<ably.PushChannelSubscription>>();
ValueStream<ably.PaginatedResult<ably.PushChannelSubscription>>
get pushChannelClientSubscriptionsStream =>
_pushChannelClientSubscriptionsSubject.stream;
final BehaviorSubject<bool> _hasPushChannelSubject =
BehaviorSubject<bool>.seeded(false);
ValueStream<bool> get hasPushChannelStream => _hasPushChannelSubject.stream;
final BehaviorSubject<ably.LocalDevice?> _localDeviceSubject =
BehaviorSubject.seeded(null);
late final ValueStream<ably.LocalDevice?> localDeviceStream =
_localDeviceSubject.stream;
final BehaviorSubject<bool> _userNotificationPermissionGrantedSubject =
BehaviorSubject();
late final ValueStream<bool> userNotificationPermissionGrantedStream =
_userNotificationPermissionGrantedSubject.stream;
final BehaviorSubject<ably.UNNotificationSettings>
_notificationSettingsSubject = BehaviorSubject();
late final ValueStream<ably.UNNotificationSettings>
notificationSettingsStream = _notificationSettingsSubject.stream;
Future<void> ensureRealtimeClientConnected() async {
if (_realtime.connection.state != ably.ConnectionState.connected) {
await _realtime.connect();
}
}
/// Only valid on iOS
Future<void> requestNotificationPermission(
{bool provisional = false,
bool providesAppNotificationSettings = true}) async {
if (useRealtimeClient) {
final granted = await _realtime.push.requestPermission(
provisional: provisional,
providesAppNotificationSettings: providesAppNotificationSettings);
_userNotificationPermissionGrantedSubject.add(granted);
} else {
final granted = await _rest.push.requestPermission(
provisional: provisional,
providesAppNotificationSettings: providesAppNotificationSettings);
_userNotificationPermissionGrantedSubject.add(granted);
}
await updateNotificationSettings();
}
/// Only valid on iOS
Future<void> updateNotificationSettings() async {
if (useRealtimeClient) {
final settings = await _realtime.push.getNotificationSettings();
_notificationSettingsSubject.add(settings);
} else {
_notificationSettingsSubject
.add(await _rest.push.getNotificationSettings());
}
}
Future<void> activateDevice() => getPushFromAblyClient().activate();
Future<void> deactivateDevice() => getPushFromAblyClient().deactivate();
Future<void> resetActivation() => getPushFromAblyClient().reset();
Future<void> getDevice() async {
if (useRealtimeClient) {
final localDevice = await _realtime.device();
_localDeviceSubject.add(localDevice);
} else {
final localDevice = await _rest.device();
_localDeviceSubject.add(localDevice);
}
}
ably.Push getPushFromAblyClient() {
if (useRealtimeClient) {
return _realtime.push;
} else {
return _rest.push;
}
}
/// Subscribes to the channel (not the push channel) which has a Push channel
/// rule. This allows the device to receive push notifications when
/// messages contain a push notification payload.
///
/// See Channel-based broadcasting for more information
/// https://ably.com/documentation/general/push/publish#channel-broadcast
Future<Stream<ably.Message?>> subscribeToChannelWithPushChannelRule() async {
await _realtimeChannelStreamSubscription?.cancel();
await ensureRealtimeClientConnected();
final stream = _realtimeChannel!.subscribe();
_realtimeChannelStreamSubscription = stream.listen((message) {
print('Message clientId: ${message.clientId}');
print('Message extras: ${message.extras}');
});
return stream;
}
StreamSubscription<ably.Message?>? _realtimeChannelStreamSubscription;
Future<Stream<ably.Message?>> subscribeToPushLogMetachannel() async {
await ensureRealtimeClientConnected();
final stream = _pushLogMetaChannel!.subscribe();
_pushLogMetaChannelSubscription = stream.listen((message) {
print('MetaChannel message');
print(message);
});
return stream;
}
StreamSubscription<ably.Message?>? _pushLogMetaChannelSubscription;
Future<void> unsubscribeToChannelWithPushChannelRule() async {
await _realtimeChannelStreamSubscription?.cancel();
}
Future<void> publishNotificationMessageToChannel() async {
await ensureRealtimeClientConnected();
if (useRealtimeClient) {
await _realtimeChannel!.publish(
message: PushNotificationMessageExamples.pushNotificationMessage);
} else {
await _restChannel!.publish(
message: PushNotificationMessageExamples.pushNotificationMessage);
}
}
Future<void> publishDataMessageToChannel() async {
await ensureRealtimeClientConnected();
if (useRealtimeClient) {
await _realtimeChannel!
.publish(message: PushNotificationMessageExamples.pushDataMessage);
} else {
await _restChannel!
.publish(message: PushNotificationMessageExamples.pushDataMessage);
}
}
Future<void> publishDataNotificationMessageToChannel() async {
await ensureRealtimeClientConnected();
if (useRealtimeClient) {
await _realtimeChannel!.publish(
message: PushNotificationMessageExamples.pushDataNotificationMessage);
} else {
await _restChannel!.publish(
message: PushNotificationMessageExamples.pushDataNotificationMessage);
}
}
void close() {
_hasPushChannelSubject.close();
_localDeviceSubject.close();
_userNotificationPermissionGrantedSubject.close();
_realtimeChannelStreamSubscription?.cancel();
_realtimeChannelStreamSubscription = null;
_pushLogMetaChannelSubscription?.cancel();
_pushLogMetaChannelSubscription = null;
}
void _getChannels() {
_hasPushChannelSubject.add(false);
if (useRealtimeClient) {
_realtimeChannel =
_realtime.channels.get(Constants.channelNameForPushNotifications);
_pushChannel = _realtimeChannel!.push;
_pushLogMetaChannel =
_realtime.channels.get(Constants.pushMetaChannelName);
_hasPushChannelSubject.add(true);
} else {
_restChannel =
_rest.channels.get(Constants.channelNameForPushNotifications);
_pushChannel = _restChannel!.push;
_hasPushChannelSubject.add(true);
}
}
/// Unfortunately ably-cocoa and ably-java are inconsistent here.
/// Ably-java will list all subscriptions (clientId and deviceId), where as
/// ably-cocoa will only give the one you specify in params.
/// This behavior is the same for [listSubscriptionsWithDeviceId]
Future<ably.PaginatedResult<ably.PushChannelSubscription>>
listSubscriptionsWithClientId() async {
await getDevice();
final subscriptions = await _pushChannel!.listSubscriptions({
'clientId': localDeviceStream.value!.clientId!,
// Optionally, limit the size of the paginated response.
// 'limit': '1'
});
_pushChannelClientSubscriptionsSubject.add(subscriptions);
return subscriptions;
}
Future<ably.PaginatedResult<ably.PushChannelSubscription>>
listSubscriptionsWithDeviceId() async {
await getDevice();
final subscriptions = await _pushChannel!.listSubscriptions({
'deviceId': localDeviceStream.value!.id!,
// Optionally, limit the size of the paginated response.
// 'limit': '1'
});
_pushChannelDeviceSubscriptionsSubject.add(subscriptions);
return subscriptions;
}
Future<void> subscribeClient() async {
await _pushChannel!.subscribeClient();
}
Future<void> unsubscribeClient() async {
await _pushChannel!.unsubscribeClient();
}
Future<void> subscribeDevice() async {
await _pushChannel!.subscribeDevice();
}
Future<void> unsubscribeDevice() async {
await _pushChannel!.unsubscribeDevice();
}
}