mattermost-push-plugin/example/lib/main.dart

87 lines
2.6 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mattermost_push_plugin/mattermost_push_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _deviceToken;
String? _lastNotification;
String? _lastOpened;
String? _lastNavigation;
StreamSubscription<Map<String, dynamic>>? _notificationSubscription;
StreamSubscription<NotificationOpenedEvent>? _openedSubscription;
@override
void initState() {
super.initState();
final plugin = MattermostPushPlugin.instance;
plugin.onDeviceTokenReady = (token) {
if (!mounted) return;
setState(() => _deviceToken = token);
};
plugin.onNavigateToChannel = (serverUrl, channelId) {
if (!mounted) return;
setState(() => _lastNavigation = 'channel:$serverUrl/$channelId');
};
plugin.onNavigateToThread = (serverUrl, rootId) {
if (!mounted) return;
setState(() => _lastNavigation = 'thread:$serverUrl/$rootId');
};
_notificationSubscription = plugin.onNotification.listen((event) {
if (!mounted) return;
setState(() => _lastNotification = _formatMap(event));
});
_openedSubscription = plugin.onNotificationOpened.listen((event) {
if (!mounted) return;
setState(() => _lastOpened = _formatOpened(event));
});
}
@override
void dispose() {
_notificationSubscription?.cancel();
_openedSubscription?.cancel();
final plugin = MattermostPushPlugin.instance;
plugin.onDeviceTokenReady = null;
plugin.onNavigateToChannel = null;
plugin.onNavigateToThread = null;
super.dispose();
}
String _formatMap(Map<String, dynamic> map) {
return map.entries.map((e) => '${e.key}: ${e.value}').join(', ');
}
String _formatOpened(NotificationOpenedEvent event) {
return 'server_url: ${event.serverUrl}, channel_id: ${event.channelId}, root_id: ${event.rootId}, is_crt_enabled: ${event.isCRTEnabled}';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Mattermost push plugin example')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Text('Device token: ${_deviceToken ?? 'pending'}'),
Text('Last notification: ${_lastNotification ?? 'none'}'),
Text('Last opened: ${_lastOpened ?? 'none'}'),
Text('Last navigation: ${_lastNavigation ?? 'none'}'),
],
),
),
);
}
}