如何使用 flutter_local_notifcations 插件取消 FCM 消息?
How do I cancel and FCM message using flutter_local_notifcations plugin?
我正在尝试确定是否可以从 iOS 和 Android 上的通知托盘中取消(清除)FCM 通知。
FCM RemoteMessage 随 RemoteNotificaiton class 一起到达,其中包含 AndroidNotification class。这个 class 只包含一个标签,而不是一个 ID。 AppleNotification 不包含标签或 ID。
flutter_local_notifcations 插件需要整数 ID,但 FCM 不在其通知中提供整数 ID。
是否可以仅在 flutter 代码中从系统托盘取消 FCM 通知?
可惜没有办法
这只能使用方法通道。
您可以使用 flutter_local_notifications -
实现相同的效果
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await flutterLocalNotificationsPlugin.cancelAll();
}
}
如果您想清除单个 FCM 通知,您可以包装 Firebase 和 flutter_local_notifications -
Create a new AndroidNotificationChannel instance:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
Create the channel on the device (if a channel with an id already exists, it will be updated):
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
Once created, we can now update FCM to use our own channel rather than the default FCM one. To do this, open the android/app/src/main/AndroidManifest.xml file for your FlutterProject project. Add the following meta-data schema within the application component:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
And then create local notification as -
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
// local notification to show to users using the created channel.
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, // Set your own Id!!
channel.name,
channel.description,
icon: android?.smallIcon,
// other properties...
),
));
}
});
现在您有了 FCM 通知的 ID,因此您可以使用该 ID 取消单个 FCM 通知。
参考:- Firebase
希望对您有所帮助。
我正在尝试确定是否可以从 iOS 和 Android 上的通知托盘中取消(清除)FCM 通知。
FCM RemoteMessage 随 RemoteNotificaiton class 一起到达,其中包含 AndroidNotification class。这个 class 只包含一个标签,而不是一个 ID。 AppleNotification 不包含标签或 ID。
flutter_local_notifcations 插件需要整数 ID,但 FCM 不在其通知中提供整数 ID。
是否可以仅在 flutter 代码中从系统托盘取消 FCM 通知?
可惜没有办法
这只能使用方法通道。
您可以使用 flutter_local_notifications -
实现相同的效果@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await flutterLocalNotificationsPlugin.cancelAll();
}
}
如果您想清除单个 FCM 通知,您可以包装 Firebase 和 flutter_local_notifications -
Create a new AndroidNotificationChannel instance:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
Create the channel on the device (if a channel with an id already exists, it will be updated):
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
Once created, we can now update FCM to use our own channel rather than the default FCM one. To do this, open the android/app/src/main/AndroidManifest.xml file for your FlutterProject project. Add the following meta-data schema within the application component:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
And then create local notification as -
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
// local notification to show to users using the created channel.
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, // Set your own Id!!
channel.name,
channel.description,
icon: android?.smallIcon,
// other properties...
),
));
}
});
现在您有了 FCM 通知的 ID,因此您可以使用该 ID 取消单个 FCM 通知。
参考:- Firebase
希望对您有所帮助。