如何停用 Firebase Messaging 中的可见通知并使用 flutter_local_notifications 包手动处理通知,以免通知显示两次?我无法编辑服务器集成端,因为我使用的是 wordpress 插件 (https://wordpress.org/plugins/fcm-push-notification-from-wp/) 为我发送通知请求。
有效载荷如下所示:https://ps.w.org/fcm-push-notification-from-wp/assets/screenshot-6.png?rev=2446404
String selectedNotificationPayload = "";
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
FirebaseMessaging messaging;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('share_icon');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {});
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
NotificationAppLaunchDetails notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) {
selectedNotificationPayload = notificationAppLaunchDetails.payload;
}
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
selectedNotificationPayload = payload;
selectNotificationSubject.add(payload);
}
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(Start());
}
class Start extends StatefulWidget {
@override
_StartState createState() => _StartState();
}
class _StartState extends State<Start>{
void initializeFlutterFire() async {
try {
await Firebase.initializeApp();
print("Firebase - initialzed successfully");
} catch (e) {
print("Firebase - initialzed FAILED");
}
messaging = FirebaseMessaging.instance;
}
void _configureSelectNotificationSubject() async {
selectNotificationSubject.stream.listen((String _url) async {
try {
await Future.delayed(Duration(milliseconds: 500), () async {
await Navigator.of(context).push(new Route(_url, ""));
});
} catch (e) {
print("Fehler beim öffnen der Website");
}
});
}
@override
initState() {
super.initState();
initializeFlutterFire();
}
...
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
Map<String, dynamic> data = message.data;
String _title = data['title'];
String _description = data['message'];
sendNotification(_title, _description);
}