Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodydavis/71761fb458b381944df32b0b78cb1d0b to your computer and use it in GitHub Desktop.
Save rodydavis/71761fb458b381944df32b0b78cb1d0b to your computer and use it in GitHub Desktop.
Firebase Messaging on Flutter Web
import 'dart:async';
import 'package:firebase/firebase.dart' as firebase;
class FBMessaging {
FBMessaging._();
static FBMessaging _instance = FBMessaging._();
static FBMessaging get instance => _instance;
firebase.Messaging _mc;
String _token;
final _controller = StreamController<Map<String, dynamic>>.broadcast();
Stream<Map<String, dynamic>> get stream => _controller.stream;
void close() {
_controller?.close();
}
Future<void> init() async {
_mc = firebase.messaging();
_mc.usePublicVapidKey('FCM_SERVER_KEY');
_mc.onMessage.listen((event) {
_controller.add(event?.data);
});
}
Future requestPermission() {
return _mc.requestPermission();
}
Future<String> getToken([bool force = false]) async {
if (force || _token == null) {
await requestPermission();
_token = await _mc.getToken();
}
return _token;
}
}
@joaovvrodrigues
Copy link

I thank you for helping the community, but following your guide on Medium I got an error and I believe that other people too, do you know what it can be?
typeerror: cannot read property ‘requestpermission’ of null at firebase_messaging.fbmessaging.__.requestpermission

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment