这是我的代码:
initializeApp() async {
FirebaseMessaging firebaseMessaging = FirebaseMessaging();
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("----->onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("----->onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("----->onResume: $message");
},
);
firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
}
这是日志:
E/FlutterFcmService( 1588): Fatal: failed to find callback
I/flutter ( 1588): onMessage: {notification: {title: this is a test, body: test}, data: {click_action: FLUTTER_NOTIFICATION_ACTION}}
This 问题也被问到这里,但没有回应。
答案 0 :(得分:1)
所以这是场景: 我有 2 个应用程序:在这两个应用程序上,我都测试过有状态小部件处于活动屏幕状态:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
class FCMTestPage extends StatefulWidget {
static String name = '/test-fcm';
@override
_FCMTestPageState createState() => _FCMTestPageState();
}
class _FCMTestPageState extends State<FCMTestPage> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
final List<Message> messages = [];
String token;
@override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
setState(() {
messages.add(Message(
title: notification['title'], body: notification['body']));
});
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
final notification = message['data'];
setState(() {
messages.add(Message(
title: '${notification['title']}',
body: '${notification['body']}',
));
});
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
}
@override
Widget build(BuildContext context) {
// getToken();
return Scaffold(
body: ListView(
children: messages.map(buildMessage).toList(),
),
);
}
Widget buildMessage(Message message) => ListTile(
title: Text(message.title),
subtitle: Text(message.body),
);
getToken() async {
token = await _firebaseMessaging.getToken();
print('********************************************');
print(token);
print('********************************************');
}
}
@immutable
class Message {
final String title;
final String body;
const Message({
@required this.title,
@required this.body,
});
}
这些适用于两个应用程序。然后我开始将我的代码从测试迁移到真正的逻辑。我有 initializeApp()
方法:
initializeApp() async {
FirebaseMessaging firebaseMessaging = FirebaseMessaging();
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
}
*删除了一些不相关的行。
有趣的是相同的代码适用于一个应用程序,不适用于另一个应用程序。相同的代码在有状态的小部件中工作,而不是在同一个应用程序中的顶级函数中工作(我试图包装在类中,使函数静态,但无济于事)。
我是如何解决的:
然后我需要快速解决问题并决定将我的一个屏幕转换为有状态并将逻辑迁移到此屏幕。 可能跟垃圾回收有关。
答案 1 :(得分:0)
将此代码添加到您的 AndroidManifest.xml 中
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>