Created
May 1, 2020 08:02
-
-
Save rodydavis/71761fb458b381944df32b0b78cb1d0b to your computer and use it in GitHub Desktop.
Firebase Messaging on Flutter Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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