54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// ignore_for_file: require_trailing_commas
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'message.dart';
|
|
|
|
/// Listens for incoming foreground messages and displays them in a list.
|
|
class MessageList extends StatefulWidget {
|
|
@override
|
|
State<StatefulWidget> createState() => _MessageList();
|
|
}
|
|
|
|
class _MessageList extends State<MessageList> {
|
|
List<RemoteMessage> _messages = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
setState(() {
|
|
_messages = [..._messages, message];
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_messages.isEmpty) {
|
|
return const Text('No messages received');
|
|
}
|
|
|
|
return ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: _messages.length,
|
|
itemBuilder: (context, index) {
|
|
RemoteMessage message = _messages[index];
|
|
|
|
return ListTile(
|
|
title: Text(
|
|
message.messageId ?? 'no RemoteMessage.messageId available'),
|
|
subtitle:
|
|
Text(message.sentTime?.toString() ?? DateTime.now().toString()),
|
|
onTap: () => Navigator.pushNamed(context, '/message',
|
|
arguments: MessageArguments(message, false)),
|
|
);
|
|
});
|
|
}
|
|
}
|