收到来自FirebaseMessaging的通知,但OnMessage()中的打印函数不起作用



我正在AndroidStudio中处理flutter项目,在收到来自firebase消息的通知时,我无法打印通知的内容。这是我的main.dart和主页的代码

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)

Logcat:

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)

另一件需要提及的事情是,当我创建一个新项目只是为了在收到通知的同时测试打印功能时,它是有效的(重写所有代码(。但是,当我从未能运行print函数的项目中复制所有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

Logcat:

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}');
})

使用数据字段显示通知

最新更新