-
Notifications
You must be signed in to change notification settings - Fork 16
/
push_notification_handlers.dart
88 lines (75 loc) · 2.99 KB
/
push_notification_handlers.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
import 'package:ably_flutter/ably_flutter.dart' as ably;
import 'package:ably_flutter_example/ui/utilities.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
class PushNotificationHandlers {
static BuildContext? context;
static final activationEvents = ably.Push.activationEvents;
static final notificationEvents = ably.Push.notificationEvents;
static final BehaviorSubject<List<ably.RemoteMessage>>
_receivedMessagesBehaviorSubject =
BehaviorSubject<List<ably.RemoteMessage>>.seeded([]);
static ValueStream<List<ably.RemoteMessage>> receivedMessagesStream =
_receivedMessagesBehaviorSubject.stream;
static void setUpEventHandlers() {
activationEvents.onUpdateFailed.listen((error) async {
logAndDisplayError(error,
prefixMessage: 'Push update registration failed');
});
activationEvents.onActivate.listen((error) async {
logAndDisplayError(error, prefixMessage: 'Push activation failed');
});
activationEvents.onDeactivate.listen((error) async {
logAndDisplayError(error, prefixMessage: 'Push deactivation failed');
});
}
static void setUpMessageHandlers() {
getLaunchMessage();
notificationEvents
..setOnBackgroundMessage(_backgroundMessageHandler)
..setOnOpenSettings(() {
print('The iOS user has asked to see the In-app Notification Settings');
});
notificationEvents.onMessage.listen((message) {
addMessage(message);
print('RemoteMessage received while app is in foreground:\n'
'RemoteMessage.Notification: ${message.notification}'
'RemoteMessage.Data: ${message.data}');
});
notificationEvents.setOnShowNotificationInForeground((message) async {
print(
'Opting to show the notification when the app is in the foreground.');
return true;
});
notificationEvents.onNotificationTap.listen((remoteMessage) {
addMessage(remoteMessage);
print('Notification was tapped: $remoteMessage');
});
}
/// You can get the notification which launched the app by a user tapping it.
static void getLaunchMessage() {
notificationEvents.notificationTapLaunchedAppFromTerminated
.then((remoteMessage) {
if (remoteMessage != null) {
addMessage(remoteMessage);
print('The app was launched by the user by tapping the notification');
print(remoteMessage.data);
}
});
}
static void clearReceivedMessages() {
_receivedMessagesBehaviorSubject.add([]);
}
static Future<void> _backgroundMessageHandler(
ably.RemoteMessage message) async {
addMessage(message);
print('RemoteMessage received while app is in background:\n'
'RemoteMessage.Notification: ${message.notification}'
'RemoteMessage.Data: ${message.data}');
}
static void addMessage(ably.RemoteMessage message) {
final newList = List<ably.RemoteMessage>.from(receivedMessagesStream.value)
..add(message);
_receivedMessagesBehaviorSubject.add(newList);
}
}