-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfirebase_messaging_web.dart
194 lines (158 loc) · 5.75 KB
/
firebase_messaging_web.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
// ignore_for_file: require_trailing_commas
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:js_interop';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_web/firebase_core_web.dart';
import 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;
import 'package:firebase_messaging_platform_interface/firebase_messaging_platform_interface.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart' as web;
import 'src/internals.dart';
import 'src/interop/messaging.dart' as messaging_interop;
import 'src/utils.dart' as utils;
/// Web implementation for [FirebaseMessagingPlatform]
/// delegates calls to messaging web plugin.
class FirebaseMessagingWeb extends FirebaseMessagingPlatform {
/// Instance of Messaging from the web plugin
messaging_interop.Messaging? _webMessaging;
messaging_interop.Messaging get _delegate {
_webMessaging ??=
messaging_interop.getMessagingInstance(core_interop.app(app.name));
if (!_initialized) {
_webMessaging!.onMessage
.listen((messaging_interop.MessagePayload webMessagePayload) {
RemoteMessage remoteMessage =
RemoteMessage.fromMap(utils.messagePayloadToMap(webMessagePayload));
FirebaseMessagingPlatform.onMessage.add(remoteMessage);
});
_initialized = true;
}
return _webMessaging!;
}
/// Called by PluginRegistry to register this plugin for Flutter Web
static void registerWith(Registrar registrar) {
FirebaseCoreWeb.registerService('messaging');
FirebaseMessagingPlatform.instance = FirebaseMessagingWeb();
}
Stream<String>? _noopOnTokenRefreshStream;
static bool _initialized = false;
/// Builds an instance of [FirebaseMessagingWeb] with an optional [FirebaseApp] instance
/// If [app] is null then the created instance will use the default [FirebaseApp]
FirebaseMessagingWeb({FirebaseApp? app}) : super(appInstance: app);
/// Updates user on browser support for Firebase.Messaging
@override
Future<bool> isSupported() {
return messaging_interop.Messaging.isSupported();
}
@override
void registerBackgroundMessageHandler(BackgroundMessageHandler handler) {}
@override
FirebaseMessagingPlatform delegateFor({required FirebaseApp app}) {
return FirebaseMessagingWeb(app: app);
}
@override
FirebaseMessagingPlatform setInitialValues({bool? isAutoInitEnabled}) {
// Not required on web, but prevents UnimplementedError being thrown.
return this;
}
@override
bool get isAutoInitEnabled {
// Not supported on web, since it automatically initializes when imported
// via the script. So return `true`.
return true;
}
@override
Future<RemoteMessage?> getInitialMessage() async {
return null;
}
@override
Future<void> deleteToken() async {
_delegate;
if (!_initialized) {
// no-op for unsupported browsers
return;
}
return convertWebExceptions(_delegate.deleteToken);
}
@override
Future<String?> getAPNSToken() async {
return null;
}
@override
Future<String?> getToken({String? vapidKey, Object? serviceWorkerRegistration}) async {
assert(serviceWorkerRegistration is web.ServiceWorkerRegistration);
_delegate;
if (!_initialized) {
// no-op for unsupported browsers
return null;
}
return convertWebExceptions(
() => _delegate.getToken(vapidKey: vapidKey, serviceWorkerRegistration: serviceWorkerRegistration as web.ServiceWorkerRegistration?),
);
}
@override
Stream<String> get onTokenRefresh {
// onTokenRefresh is deprecated on web, however since this is a non-critical
// api we just return a noop stream to keep functionality the same across
// platforms.
return _noopOnTokenRefreshStream ??=
StreamController<String>.broadcast().stream;
}
@override
Future<NotificationSettings> getNotificationSettings() async {
return utils.getNotificationSettings(web.Notification.permission);
}
@override
Future<NotificationSettings> requestPermission({
bool alert = true,
bool announcement = false,
bool badge = true,
bool carPlay = false,
bool criticalAlert = false,
bool provisional = false,
bool sound = true,
bool providesAppNotificationSettings = false,
}) {
return convertWebExceptions(() async {
String status =
(await web.Notification.requestPermission().toDart).toDart;
return utils.getNotificationSettings(status);
});
}
@override
Future<void> setAutoInitEnabled(bool enabled) async {
// Noop out on web - not supported but no need to crash
return;
}
@override
Future<void> setForegroundNotificationPresentationOptions({
required bool alert,
required bool badge,
required bool sound,
}) async {
// TODO(rrousselGit) dead code? Should this throw an UnimplementedError?
return;
}
@override
Future<void> subscribeToTopic(String topic) {
throw UnimplementedError('''
subscribeToTopic() is not supported on the web clients.
To learn how to manage subscriptions for web users, visit the
official Firebase documentation:
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
''');
}
@override
Future<void> unsubscribeFromTopic(String topic) {
throw UnimplementedError('''
unsubscribeFromTopic() is not supported on the web clients.
To learn how to manage subscriptions for web users, visit the
official Firebase documentation:
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
''');
}
}