是否可以检索和利用 FCM 通知中的数据?

问题描述

我有一个初始化 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...