[英]FCM push notifications FLUTTER when app is terminated
I am trying to receive push notifications on ANDROID from the Firebase console when the app is terminated but I get nothing (I have onResume, onMessage and onLaunch callback listeners but I don't even want to handle them in the app yet) I'd just like to get them to show In the tray or lock screen.当应用程序终止时,我试图从 Firebase 控制台接收有关 ANDROID 的推送通知,但我什么也没得到(我有 onResume、onMessage 和 onLaunch 回调侦听器,但我什至不想在应用程序中处理它们)就像让它们显示在托盘或锁定屏幕中。 Is there some Android configuration required?
是否需要一些 Android 配置?
first answer, I hope will be good.第一个答案,我希望会很好。
As you can seehere notifications are handled with onBackgroundMessage even if the app is terminated.正如您在此处看到的,即使应用程序终止,通知也会使用 onBackgroundMessage 处理。 Maybe your problem is that you are not sending a Notification but a "Data only" message.
也许您的问题是您没有发送通知,而是发送“仅数据”消息。 In that case you have to set the field
"priority": "high"
在这种情况下,您必须设置字段
"priority": "high"
Also, with the latest release of Flutter and firebase_messaging the methods onResume, onMessage and onLaunch are deprecated.此外,在最新版本的 Flutter 和firebase_messaging中,方法 onResume、onMessage 和 onLaunch 已弃用。 You should substitute them with onMessage, onMessageOpened, onBackground, but you can find definitely more info with the official documentation
您应该将它们替换为 onMessage、onMessageOpened、onBackground,但您可以通过官方文档找到更多信息
Just an addition to @samUser1 answer.只是对@samUser1 答案的补充。 When app is terminated this code will handle events.
当应用程序终止时,此代码将处理事件。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
And outside main function parse it.在主 function 之外解析它。 The example below uses AwesomeNotifications package.
下面的示例使用 AwesomeNotifications package。 https://github.com/rafaelsetragni/awesome_notifications/blob/master/example/lib/main.dart
https://github.com/rafaelsetragni/awesome_notifications/blob/master/example/lib/main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
final res = await AwesomeNotifications().createNotification(
content: NotificationContent(
id: Random().nextInt(1000000),
channelKey: 'channel_name',
title: 'Hello!',
body: 'World!',
notificationLayout: NotificationLayout.Default,
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.