Skip to content

Instantly share code, notes, and snippets.

@TesteurManiak
Created October 15, 2020 07:38
Show Gist options
  • Select an option

  • Save TesteurManiak/e053b974905b8dd32b7116a74f2424a7 to your computer and use it in GitHub Desktop.

Select an option

Save TesteurManiak/e053b974905b8dd32b7116a74f2424a7 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_fcm_web_example/notification_encapsulation.dart';
import 'package:flutter_fcm_web_example/notification_model.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isLoading = true;
StreamSubscription<NotificationModel> _streamSubscription;
String _token = '';
final _notifInstance = NotificationEncapsulation.instance;
@override
void initState() {
super.initState();
_notifInstance.isReady.then((isReady) async {
if (isReady) {
_token = await _notifInstance.getToken();
print(_token);
_streamSubscription = _notifInstance.stream.listen((event) {
if (event == null) return;
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(event.title),
content: Text(event.body),
),
);
});
}
setState(() => _isLoading = false);
});
}
@override
void dispose() {
_streamSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(16),
child: _isLoading
? CircularProgressIndicator()
: Text(
'FCM token: $_token',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment