我正在 AndroidStudio 中处理 flutter 项目,在接收来自 firebase 消息传递的通知时无法打印通知内容。这是我的 main.dart 和 homePage 的代码
main.dart代码:
void main()async
{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
const ConnectionPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return
MaterialApp
(
title: "HomeHero",
theme: ThemeData
(
primaryColor: Colors.white,
backgroundColor: Colors.white,
),
home: homePage(),
);
);
}
}
首页代码:
class homePage extends StatefulWidget {
@override
_homePage createState() => _homePage();
}
class _homePage extends State<homePage>
{
void iniState() {
super.initState();
FirebaseMessaging.instance.getInitialMessage();
FirebaseMessaging.onMessage.listen((message) {
print(message.notification.body);
print(message.notification.title);
});
}
输出:
D/FLTFireMsgReceiver(23787): broadcast received for message
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
日志猫:
2022-03-22 14:14:20.150 23787-23787/com.example.providers_app D/FLTFireMsgReceiver: broadcast received for message
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
2022-03-22 14:14:20.194 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
另一件事要提的是,当我创建一个新项目只是为了在接收通知时测试打印功能时,它可以工作(重写所有代码)。但是当我从项目中复制所有无法运行打印功能的lib文件并将其粘贴到新创建的项目的lib中时,它再次失败。
我重写项目的代码:
void main()async
{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(ConnectionPage());
}
class ConnectionPage extends StatelessWidget
{
const ConnectionPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return
MaterialApp
(
title: "HomeHero",
theme: ThemeData
(
primaryColor: Colors.white,
backgroundColor: Colors.white,
),
home: MyHomePage(),
);
}
我的主页代码:
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
// TODO: implement initState
super.initState();
FirebaseMessaging.instance.getInitialMessage();
FirebaseMessaging.onMessage.listen((message) {
print(message.notification.body);
print(message.notification.title);
});
}
}
这是重写项目的输出:
D/FLTFireMsgReceiver(24304): broadcast received for message
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
I/flutter (24304): Notification Testing
I/flutter (24304): Testing
日志猫:
2022-03-22 14:45:04.205 24304-24304/com.example.notification_testing D/FLTFireMsgReceiver: broadcast received for message
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
2022-03-22 14:45:04.276 24304-24351/com.example.notification_testing I/flutter: Notification Testing
2022-03-22 14:45:04.278 24304-24351/com.example.notification_testing I/flutter: Testing
FCM 主体
{
"to": "<DEVICE ID>",
"notification": {
"body": "Body of Your Notification",
"title": "Title of Your Notification A",
"sound": "default",
"alert": "New"
},
"priority": "high",
"contentAvailable": true,
"data": {
"body": "Body of Your Notification in Data",
"title": "Title of Your Notification in Title B",
"key_1": "Value for key_1",
"key_2": "Value for key_2"
}
}
FirebaseMessaging.onMessage.listen((message) {
print(' FCM on message : ${message.data}');
})
使用数据字段显示通知
同样的挑战困扰了我好几天。 我使用由
overlay_support: ^2.1.0
包提供支持的应用内通知小部件解决了这个问题。这是该包的链接https://pub.dev/packages/overlay_support
此软件包使人们能够通过应用程序显示通知。在可自定义的持续时间后将其关闭。
用这个小部件包装你的MaterialApp()之后,
...
return OverlaySupport(
child: MaterialApp(
title: 'App Title',
scaffoldMessengerKey: scaffoldMessengerKey ,
routes:<String, WidgetBuilder> {
},
),
);
您可以通过调用
onMessage
对象上的 FirebaseMessaging
从任何屏幕启动通知。
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print("onMessage: $message");
showOverlayNotification((context) {
return Card(
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 50, horizontal: 15),
child: ListTile(
minVerticalPadding: 0,
contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
leading: SizedBox.fromSize(
size: const Size(40, 40),
child: ClipOval( child: Image.asset('assets/images/icon.png'))
),
// title: Text(message.notification!.title??""),
subtitle: Text(message.notification!.body??""),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: () {
OverlaySupportEntry.of(context)!.dismiss();
}),
),
);
}, duration: Duration(milliseconds: 4000));
});