Flutter本地通知-当应用程序在后台运行时,通知单击不工作



当应用程序被杀死时,通知点击不工作帐户onSelectNotification和大图图像通知当应用程序在后台时不工作

flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (action) {})

通过注册onBackgroundMessage处理程序来处理后台消息。当接收到消息时,将生成一个隔离(仅限Android, iOS/macOS不需要单独的隔离),允许您在应用程序未运行时处理消息。试试这个:

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}

在这里查看完整的文档:https://firebase.google.com/docs/cloud-messaging/flutter/receive

没有"背景"用移动设备。移动设备运行一个前台应用。当你把它放到后台时它是关闭的,并保留了一个截图,让你认为它"在后台"。它不是。关闭。

所以当它关闭时它不起作用。这是正常的。因为应用程序没有运行,所以它不能执行代码。

应用程序必须首先重新启动。要知道你的应用程序是否通过点击通知启动,你可以在启动代码中使用这一行:

final NotificationAppLaunchDetails notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

来源:文档

这样你就可以知道你的应用程序是否从你的通知启动,然后采取相应的行动(例如,根据这些细节导航到不同的路由)。

最新更新