[英]Firebase push notifications callback doesn't work when app is terminated
So I updated the firebase_messaging and I had to change my code because FirebaseMessagin.configure()
is deprecated and now when I receive the notification and click on the notification it doesn't open another screen.所以我更新了firebase_messaging并且我不得不更改我的代码,因为FirebaseMessagin.configure()
已被弃用,现在当我收到通知并单击通知时,它不会打开另一个屏幕。
This is how I implemented the notifications:这就是我实现通知的方式:
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');
});
}
}
But .onMessageOpenedApp
isn't called when I click on the notification because I don't get that debugPrint
message in my console (VSCode) and I get the following errors:但是,当我单击通知时,不会调用.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
I sent my notification from the firebase with the click_action: FLUTTER_NOTIFICATION_CLICK
and in my manifest I've added我通过click_action: FLUTTER_NOTIFICATION_CLICK
从 firebase 发送了我的通知,并在我的清单中添加了
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
My firebase_messaging
version is ^8.0.0-dev.15
我的firebase_messaging
版本是^8.0.0-dev.15
So I don't know what I've missed or why it's not working.所以我不知道我错过了什么或为什么它不起作用。 If you need more details please feel free to ask.如果您需要更多详细信息,请随时询问。
I resolved this by using the .getInitialMessage()
function (This is the callback if the app is terminated. My notifications worked when the app was on background but not terminated.我通过使用.getInitialMessage()
function 解决了这个问题(这是应用程序终止时的回调。当应用程序在后台但未终止时,我的通知有效。
To resolve this I just added this to my code:为了解决这个问题,我刚刚将它添加到我的代码中:
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
Navigator.pushNamed(context, message.data['view']);
}
});
It should work when the app is in the background, but when it is terminated you should use getInitialMessage
.当应用程序在后台时它应该可以工作,但是当它终止时你应该使用getInitialMessage
。
onMessageOpenedApp
: A Stream event will be sent if the app has opened from a background state (not terminated).onMessageOpenedApp
:如果应用程序已从后台 state(未终止)打开,则将发送 Stream 事件。If your app is opened via a notification whilst the app is terminated, see
getInitialMessage
.如果您的应用在应用终止时通过通知打开,请参阅getInitialMessage
。
Check out the example: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart#L116查看示例: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart#L116
if you want go to any page or lunch link when click notification before app started then you must use如果您希望 go 在应用程序启动之前单击通知时进入任何页面或午餐链接,那么您必须使用
getInitialMessage()
example:例子:
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
//to do your operation
launch('https://google.com');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.