You*_*ang 5 notifications push-notification flutter
我的应用程序有聊天功能。我已经实现了 Firebase 云消息传递和本地通知。
\n因此,当有人向我发送消息时,会显示通知(一个位于 Android 状态栏内)和本地通知(一个位于屏幕上部,持续约 3 秒)。它们会在应用程序打开、在后台以及应用程序终止时出现。(到目前为止,一切都很好)
\n但当我已经在与该人的聊天页面中时,我不希望同时显示这两个通知(1. fcm 通知,2. 本地通知)。我不知道从哪里开始。
\n下面是我处理 fcm 并显示本地通知的代码。它们位于 main.dart 内部,主函数在返回我的根页面之前调用initializeFCM。
\nFuture<void> initializeFCM() async {\n await Firebase.initializeApp();\n\n FirebaseMessaging messaging = FirebaseMessaging.instance;\n\n // Firebase Messaging\n NotificationSettings settings = await messaging.requestPermission(\n alert: true,\n announcement: false,\n badge: true,\n carPlay: false,\n criticalAlert: false,\n provisional: false,\n sound: true,\n );\n\n if (settings.authorizationStatus == AuthorizationStatus.authorized) {\n // print('User granted permission');\n } else\n if (settings.authorizationStatus == AuthorizationStatus.provisional) {\n // print('User granted provisional permission');\n } else {\n // print('User declined or has not accepted permission');\n }\n\n\n\n // Get any messages which caused the application to open from\n // a terminated state.\n RemoteMessage? initialMessage = await messaging.getInitialMessage();\n\n if (initialMessage != null) {\n handleMessage(initialMessage);\n }\n\n\n // initialize local notification\n final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();\n\n // terminated app\xec\x9d\x84 local notification \xeb\x88\x8c\xeb\x9f\xac\xec\x84\x9c \xec\x97\xb4\xec\x97\x88\xec\x9d\x84 \xeb\x95\x8c - payload\xea\xb0\x80 \xec\xa0\x95\xec\x83\x81\xec\xa0\x81\xec\x9c\xbc\xeb\xa1\x9c \xeb\x8f\x99\xec\x9e\x91\xed\x95\x98\xec\xa7\x80 \xec\x95\x8a\xec\x9d\x8c\n final NotificationAppLaunchDetails? notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();\n final didNotificationLaunchApp = notificationAppLaunchDetails?.didNotificationLaunchApp ?? false;\n if (didNotificationLaunchApp) {\n onSelectNotification(notificationAppLaunchDetails!.payload);\n }\n\n\n FirebaseMessaging.onMessage.listen((RemoteMessage message) {\n showLocalNotification(message);\n });\n\n FirebaseMessaging.onBackgroundMessage(showLocalNotification);\n\n FirebaseMessaging.onMessageOpenedApp.listen(handleMessage);\n}\n\n\nvoid handleMessage(RemoteMessage message) {\n if (message.data["screen"] == "message") {\n Navigator.push(\n navigatorKey.currentContext!,\n MaterialPageRoute(\n builder: (context) => MessageListPage(),\n )\n );\n }\n}\n\nFuture<void> onSelectNotification(payload) async {\n if(payload != null) {\n Map<String, dynamic> data = json.decode(payload);\n\n if (data['screen'] == "message") {\n Navigator.push(\n navigatorKey.currentContext!,\n MaterialPageRoute(\n builder: (context) => MessageListPage(),\n )\n );\n }\n }\n}\n\n\nFuture<void> showLocalNotification(RemoteMessage message) async {\n WidgetsFlutterBinding.ensureInitialized();\n await Firebase.initializeApp();\n\n RemoteNotification? notification = message.notification;\n AndroidNotification? android = message.notification?.android;\n // String screen = message.data['screen'];\n\n // iOS heads up notification setting\n await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(\n alert: true, // Required to display a heads up notification\n badge: true,\n sound: true,\n );\n\n // android channel setting\n const AndroidNotificationChannel channel = AndroidNotificationChannel(\n 'high_importance_channel', // id\n 'High Importance Notifications', // title\n importance: Importance.max,\n );\n\n // initialize local notification\n final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();\n\n await flutterLocalNotificationsPlugin.initialize(InitializationSettings(\n android: AndroidInitializationSettings('@drawable/mentea_ic_stat_name'),\n iOS: IOSInitializationSettings()),\n onSelectNotification: onSelectNotification);\n\n await flutterLocalNotificationsPlugin\n .resolvePlatformSpecificImplementation<\n AndroidFlutterLocalNotificationsPlugin>()\n ?.createNotificationChannel(channel);\n\n // If `onMessage` is triggered with a notification, construct our own\n // local notification to show to users using the created channel.\n if (notification != null && android != null) {\n // print('Message also contained a notification: ${message.notification}');\n flutterLocalNotificationsPlugin.show(\n notification.hashCode,\n notification.title,\n notification.body,\n NotificationDetails(\n android: AndroidNotificationDetails(\n 'high_importance_channel', //channel.id,\n 'High Importance Notifications', //channel.name,\n icon: android.smallIcon,\n color: primaryColor,\n // other properties...\n ),\n ),\n payload: json.encode(message.data)\n );\n }\n}\n\nRun Code Online (Sandbox Code Playgroud)\n
小智 1
我不知道你是否仍然遇到这个问题,但我会在这里发布我的解决方案。
碰巧的是,当我们使用通知消息功能发送推送通知时,如果 App 处于后台并已完成,FCM 插件会自动在设备上发送通知,此时我们只能通过本地通知或其他解决方案自定义前台通知。该应用程序。因此Firebase文档中针对这种情况指出的解决方案是仅将推送通知作为数据消息发送,而不包含通知对象。下面我展示了示例和文档链接。
组合通知示例:
const payload = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
data: {
stock: 'GOOG',
open: '829.62',
close: '635.67'
}
};
Run Code Online (Sandbox Code Playgroud)
仅包含数据消息的通知示例:
const payload = {
data: {
score: '850',
time: '2:45'
}
};
Run Code Online (Sandbox Code Playgroud)
仅包含通知消息的通知示例:
const payload = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
}
};
Run Code Online (Sandbox Code Playgroud)
因此,您可以自定义所有形式的通知(前台、后台和终止),只需通过后端将推送通知作为数据消息发送即可。
| 归档时间: |
|
| 查看次数: |
3031 次 |
| 最近记录: |