问题描述
我有一个初始化 FCM 的 Java 服务器,我有一个名为 sendToToken() 的方法,它获取设备的注册令牌并使用它来发送通知,我还有一个 JavaScript接收这些通知的客户端。这是FCMServer.java我的代码:
public void sendToToken() throws FirebaseMessagingException {
String registrationToken = "xxxxxxxxxxxxx";
Message message = Message.builder()
.setNotification(new Notification("Price drop","5% off all electronics"))
.putData("score","850")
.putData("time","2:45")
.setToken(registrationToken)
.build();
String response = FirebaseMessaging.getInstance().send(message);
System.out.println("Successfully sent message: " + response);
}
这是我的 firebase-messaging-sw.js 代码。
importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-messaging.js');
importScripts('https://storage.googleapis.com/workBox-cdn/releases/6.0.2/workBox-sw.js');
var firebaseConfig = {
apiKey: "xxxx",authDomain: "xxxx",databaseURL: "xxxxx",projectId: "xxxxxxxxxxxxx",storageBucket: "xxxxxxxxxxxxxxxx",messagingSenderId: "xxxxxxxxxxxxxxxxxxx",appId: "xxxxxxxxxxxxxxxxxx",measurementId: "xxxxxxxxxxxxxxxxx"
};
firebase.initializeApp(firebaseConfig);
const messaging =firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload){
console.log('[firebase-messaging-sw.js] Received background message ',payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationoptions = {
body: 'Background Message body.',icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,notificationoptions);
});
messaging.onMessage((payload) => {
console.log('Message received. ',payload);
// ...
});
现在,当我发送消息时,客户端正确地接收到它,但我无法与通知中的信息进行交互,我想知道这是否可能。我是 FCM 的新手。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)