【发布时间】:2021-07-03 01:22:01
【问题描述】:
所以我更新了firebase_messaging,我不得不更改我的代码,因为FirebaseMessagin.configure() 已被弃用,现在当我收到通知并单击通知时,它不会打开另一个屏幕。
这就是我实现通知的方式:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'e-Rădăuți',
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: {
'/': (_) => MenuScreen(),
'/events': (BuildContext context) => EventsScreen(),
},
);
}
}
class MenuScreen extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
Widget build(BuildContext context) {
return Scaffold();
}
@override
void initState() {
super.initState();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
debugPrint('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/events');
});
}
}
但是当我点击通知时不会调用 .onMessageOpenedApp,因为我在控制台 (VSCode) 中没有收到 debugPrint 消息并且我收到以下错误:
D/FLTFireMsgReceiver( 4799): broadcast received for message
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
W/FirebaseMessaging( 4799): Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.
I/flutter ( 4799): Handling a background message 0:1617783965733220%2ebdcc762ebdcc76
我使用 click_action: FLUTTER_NOTIFICATION_CLICK 从 firebase 发送了通知,并在我的清单中添加了
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
我的firebase_messaging 版本是^8.0.0-dev.15
所以我不知道我错过了什么或为什么它不起作用。如果您需要更多详细信息,请随时询问。
【问题讨论】:
-
嗨,我对 firebasemessaging ^9.1.3 也有同样的问题,当点击通知并且应用程序处于前台时,不会触发 FirebaseMessaging.onMessageOpenedApp.listen。你找到解决办法了吗?
-
@ALEXANDERLOZANO 很抱歉回复晚了。我认为如果应用程序在前台,你应该使用
.onMessage.listen' because the app is already opened and in foreground so you actually don't open the app soFirebaseMessaging.onMessageOpenedApp.listen`,而不是调用FirebaseMessaging.onMessage.listen。我不确定这是否可行,因为当应用程序处于前台时我没有实现回调。如果您没有找到解决方案,请发消息给我,我会尝试在我的代码中实现,看看它是否可以工作 -
感谢您的回答,我使用:FirebaseMessaging.onMessage.listen((RemoteMessage message) { print("new message"); });没有运气。通知到达但未触发监听
-
我在
.onMessage中添加了一个debugPrint();,如果应用程序在前台会触发它,但当我点击它时不会触发它,当我收到通知时会触发它。如果我找到解决方案,我会告诉你 -
@ALEXANDERLOZANO 所以看来你必须添加本地推送通知并使用它来在应用程序处于前台时进行回调..我明天会制作一个演示应用程序..它是 1:上午 40 点:(
标签: android flutter push-notification firebase-cloud-messaging remote-notifications