在我的应用程序中,对于web版本,我使用包firebase 7.3.0
用于Firebase服务,现在我也在为web设置FCM。当我在前台收到应用程序的新消息时,messaging()
中的onMessage:
会被触发两次。在某个Flutter版本之前,flutter_messaging
设备插件也曾发生过这种情况,但现在已经解决了。
我基本上设置了一个StreamTransformer
来获取与PlatformPushNotificationDevice
中使用的flutter_messaging
设备包相同类型的Map<String, dynamic>
中的消息,我使用Stub根据平台切换类。在web类PlatformPushNotificationWeb
中,我将消息传递实例化为var firebaseMessaging = messaging();
,并声明我的方法,其中之一是onMessage()
:
Stream<Map<String, dynamic>> onMessage() async* {
print('PlatformPushNotificationWeb.onMessage() started');
handleData(Payload payload, EventSink<Map<String, dynamic>> sink) {
Map<String,dynamic> message = {
'notification': {
'title': payload.notification.title,
'body': payload.notification.body,
'sound': true
},
'data': payload.data
};
sink.add(message);
}
final transformer = StreamTransformer<Payload, Map<String, dynamic>>.fromHandlers(
handleData: handleData);
yield* firebaseMessaging.onMessage.transform(transformer);
}
因此,无论平台是,我所在集团的听众都会收到相同的消息类型
Stream<PushNotificationState> _mapListenToMessagesToState(ListenToMessages event) async* {
print('_mapListenToMessagesToState started');
_pushNotificationSwitcher.onMessage().listen((message) {
print('_mapListenToMessagesToState onMessage() listener: received new message');
add(ReceivedNewMessage(message: message));
});
}
但对于每一条传入的消息,我都会让侦听器响应两次。
我将index.html
设置为:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fixit cloud biking</title>
<!-- <meta name="google-signin-client_id" content="YOUR_GOOGLE_SIGN_IN_OAUTH_CLIENT_ID.apps.googleusercontent.com">-->
<meta name="google-signin-client_id" content="xxxx.apps.googleusercontent.com">
<!-- <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">-->
</head>
<!--<body>-->
<body id="app-container">
<script src="main.dart.js?version=45" type="application/javascript"></script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-remote-config.js"></script>
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
//navigator.serviceWorker.register("/flutter_service_worker.js");
navigator.serviceWorker.register("/firebase-messaging-sw.js");
});
}
</body>
</html>
firebase-messaging-sw.js
文件为:
importScripts("https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.19.1/firebase-messaging.js");
firebase.initializeApp({
apiKey: "xxxx",
authDomain: "xxxx",
databaseURL: "xxxx",
projectId: "xxx",
storageBucket: "xxxx",
messagingSenderId: "xxxx",
appId: "xxx",
measurementId: "G-xxxx",
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
const promiseChain = clients
.matchAll({
type: "window",
includeUncontrolled: true
})
.then(windowClients => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(payload);
}
})
.then(() => {
return registration.showNotification("New Message");
});
return promiseChain;
});
self.addEventListener('notificationclick', function (event) {
console.log('notification received: ', event)
});
在PlatformPushNotificationWeb
类和firebase-messaging-sw.js
文件中实例化消息是否是onMessage:
回调被触发两次的原因?非常感谢。
事实证明。。一个很好的颤振清理,安卓工作室和机器重启解决了它。
现在我不会两次收到同一条消息,但对于那些真正尝试重复消息的人来说,这将解决问题,面团只是一种变通方法。
刚刚声明了int lastMessageId = 0
变量。当消息传入时,请将其id
与之进行核对,只有当它们不同时,才将新消息id保存为lastMessageId
并显示消息。
Stream<PushNotificationState> _mapListenToMessagesToState(ListenToMessages event) async* {
print('_mapListenToMessagesToState started');
int lastMessageId = 0;
_pushNotificationSwitcher.onMessage().listen((message) {
print('incoming message is : $message');
var data = message['data'];
int messageId = int.parse(data['id']);
if( lastMessageId != messageId) {
lastMessageId = messageId;
print(
'_mapListenToMessagesToState onMessage() listener: received new message');
add(ReceivedNewMessage(message: message));
}
});
}