-
Notifications
You must be signed in to change notification settings - Fork 4k
/
firebase_messaging.dart
234 lines (203 loc) · 7.78 KB
/
firebase_messaging.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
// Copyright 2017 The Chromium Authors. 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:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, TargetPlatform;
typedef Future<dynamic> MessageHandler(Map<String, dynamic> message);
/// Setup method channel to handle Firebase Cloud Messages received while
/// the Flutter app is not active. The handle for this method is generated
/// and passed to the Android side so that the background isolate knows where
/// to send background messages for processing.
///
/// Your app should never call this method directly, this is only for use
/// by the firebase_messaging plugin to setup background message handling.
void _fcmSetupBackgroundChannel(
{MethodChannel backgroundChannel = const MethodChannel(
'plugins.flutter.io/firebase_messaging_background')}) async {
// Setup Flutter state needed for MethodChannels.
WidgetsFlutterBinding.ensureInitialized();
// This is where the magic happens and we handle background events from the
// native portion of the plugin.
backgroundChannel.setMethodCallHandler((MethodCall call) async {
if (call.method == 'handleBackgroundMessage') {
final CallbackHandle handle =
CallbackHandle.fromRawHandle(call.arguments['handle']);
final Function handlerFunction =
PluginUtilities.getCallbackFromHandle(handle);
try {
await handlerFunction(
Map<String, dynamic>.from(call.arguments['message']));
} catch (e) {
print('Unable to handle incoming background message.');
print(e);
}
return Future<void>.value();
}
});
// Once we've finished initializing, let the native portion of the plugin
// know that it can start scheduling handling messages.
backgroundChannel.invokeMethod<void>('FcmDartService#initialized');
}
/// Implementation of the Firebase Cloud Messaging API for Flutter.
///
/// Your app should call [requestNotificationPermissions] first and then
/// register handlers for incoming messages with [configure].
class FirebaseMessaging {
factory FirebaseMessaging() => _instance;
@visibleForTesting
FirebaseMessaging.private(MethodChannel channel) : _channel = channel;
static final FirebaseMessaging _instance = FirebaseMessaging.private(
const MethodChannel('plugins.flutter.io/firebase_messaging'));
final MethodChannel _channel;
MessageHandler _onMessage;
MessageHandler _onBackgroundMessage;
MessageHandler _onLaunch;
MessageHandler _onResume;
/// On iOS, prompts the user for notification permissions the first time
/// it is called.
///
/// Does nothing and returns null on Android.
FutureOr<bool> requestNotificationPermissions([
IosNotificationSettings iosSettings = const IosNotificationSettings(),
]) {
if (defaultTargetPlatform != TargetPlatform.iOS) {
return null;
}
return _channel.invokeMethod<bool>(
'requestNotificationPermissions',
iosSettings.toMap(),
);
}
final StreamController<IosNotificationSettings> _iosSettingsStreamController =
StreamController<IosNotificationSettings>.broadcast();
/// Stream that fires when the user changes their notification settings.
///
/// Only fires on iOS.
Stream<IosNotificationSettings> get onIosSettingsRegistered {
return _iosSettingsStreamController.stream;
}
/// Sets up [MessageHandler] for incoming messages.
void configure({
MessageHandler onMessage,
MessageHandler onBackgroundMessage,
MessageHandler onLaunch,
MessageHandler onResume,
}) {
_onMessage = onMessage;
_onLaunch = onLaunch;
_onResume = onResume;
_channel.setMethodCallHandler(_handleMethod);
_channel.invokeMethod<void>('configure');
if (onBackgroundMessage != null) {
_onBackgroundMessage = onBackgroundMessage;
final CallbackHandle backgroundSetupHandle =
PluginUtilities.getCallbackHandle(_fcmSetupBackgroundChannel);
final CallbackHandle backgroundMessageHandle =
PluginUtilities.getCallbackHandle(_onBackgroundMessage);
if (backgroundMessageHandle == null) {
throw ArgumentError(
'''Failed to setup background message handler! `onBackgroundMessage`
should be a TOP-LEVEL OR STATIC FUNCTION and should NOT be tied to a
class or an anonymous function.''',
);
}
_channel.invokeMethod<bool>(
'FcmDartService#start',
<String, dynamic>{
'setupHandle': backgroundSetupHandle.toRawHandle(),
'backgroundHandle': backgroundMessageHandle.toRawHandle()
},
);
}
}
final StreamController<String> _tokenStreamController =
StreamController<String>.broadcast();
/// Fires when a new FCM token is generated.
Stream<String> get onTokenRefresh {
return _tokenStreamController.stream;
}
/// Returns the FCM token.
Future<String> getToken() async {
return await _channel.invokeMethod<String>('getToken');
}
/// Subscribe to topic in background.
///
/// [topic] must match the following regular expression:
/// "[a-zA-Z0-9-_.~%]{1,900}".
Future<void> subscribeToTopic(String topic) {
return _channel.invokeMethod<void>('subscribeToTopic', topic);
}
/// Unsubscribe from topic in background.
Future<void> unsubscribeFromTopic(String topic) {
return _channel.invokeMethod<void>('unsubscribeFromTopic', topic);
}
/// Resets Instance ID and revokes all tokens. In iOS, it also unregisters from remote notifications.
///
/// A new Instance ID is generated asynchronously if Firebase Cloud Messaging auto-init is enabled.
///
/// returns true if the operations executed successfully and false if an error ocurred
Future<bool> deleteInstanceID() async {
return await _channel.invokeMethod<bool>('deleteInstanceID');
}
/// Determine whether FCM auto-initialization is enabled or disabled.
Future<bool> autoInitEnabled() async {
return await _channel.invokeMethod<bool>('autoInitEnabled');
}
/// Enable or disable auto-initialization of Firebase Cloud Messaging.
Future<void> setAutoInitEnabled(bool enabled) async {
await _channel.invokeMethod<void>('setAutoInitEnabled', enabled);
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onToken":
final String token = call.arguments;
_tokenStreamController.add(token);
return null;
case "onIosSettingsRegistered":
_iosSettingsStreamController.add(IosNotificationSettings._fromMap(
call.arguments.cast<String, bool>()));
return null;
case "onMessage":
return _onMessage(call.arguments.cast<String, dynamic>());
case "onLaunch":
return _onLaunch(call.arguments.cast<String, dynamic>());
case "onResume":
return _onResume(call.arguments.cast<String, dynamic>());
default:
throw UnsupportedError("Unrecognized JSON message");
}
}
}
class IosNotificationSettings {
const IosNotificationSettings({
this.sound = true,
this.alert = true,
this.badge = true,
this.provisional = false,
});
IosNotificationSettings._fromMap(Map<String, bool> settings)
: sound = settings['sound'],
alert = settings['alert'],
badge = settings['badge'],
provisional = settings['provisional'];
final bool sound;
final bool alert;
final bool badge;
final bool provisional;
@visibleForTesting
Map<String, dynamic> toMap() {
return <String, bool>{
'sound': sound,
'alert': alert,
'badge': badge,
'provisional': provisional
};
}
@override
String toString() => 'PushNotificationSettings ${toMap()}';
}