Javascript 在react js上为Fcm通知设置onClick

Javascript 在react js上为Fcm通知设置onClick,javascript,reactjs,push-notification,firebase-cloud-messaging,Javascript,Reactjs,Push Notification,Firebase Cloud Messaging,我试图将url传递给我的fcm通知,因此当用户单击通知时,将打开一个网页。我的服务人员如下 importScripts("https://www.gstatic.com/firebasejs/7.2.3/firebase-app.js"); importScripts("https://www.gstatic.com/firebasejs/7.2.3/firebase-messaging.js"); const FIREBASE_CONFIG = {

我试图将url传递给我的fcm通知,因此当用户单击通知时,将打开一个网页。我的服务人员如下


importScripts("https://www.gstatic.com/firebasejs/7.2.3/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.2.3/firebase-messaging.js");

const FIREBASE_CONFIG = {
  apiKey: "dssadsadcdcasdc",
  authDomain: "asdsadsadsadas",
  databaseURL: "sdadadsad",
  projectId: "dasdsadsad",
  storageBucket: "dsadacsadcdac",
  messagingSenderId: "sadcasdcas",
  appId: "cxzcxzcxz x zc",
  measurementId: "cxzcxzczxczx",
};
firebase.initializeApp(FIREBASE_CONFIG);

firebase.messaging();
我还有一个叫做NotificationNotificationManager的类,它正在做下面的工作


registerFcmListener = () => {
    navigator.serviceWorker
      .register("/static-files/firebase-messaging-sw.js")
      .then((registration) => {
        firebase.initializeApp(settings.getConfig().FIREBASE_CONFIG);
        const messaging = firebase.messaging();
        messaging.useServiceWorker(registration);

        // Request permission and get token.....
        try {
          messaging
            .requestPermission()
            .then(() => {
              return messaging.getToken();
            })
            .then((token) => {
              // let topic = `${userInfo.is_host ? "host" : "guest"}`;

              // this.subscribeToTopic(topic, token);
              this.sendTokenToServer({
                os: "web",
                push_token: token,
              });
            });
        } catch (error) {
          if (error.code === "messaging/permission-blocked") {
            console.log("Please Unblock Notification Request Manually");
          } else {
            console.log("Error Occurred", error);
          }
        }

        messaging.onMessage((payload) => {
          console.log("ddd", payload);
          self.addEventListener("notificationclick", function (event) {
            let url = "https://google.com/";
            event.notification.close(); // Android needs explicit close.
            event.waitUntil(
              clients.matchAll({ type: "window" }).then((windowClients) => {
                // Check if there is already a window/tab open with the target URL
                for (var i = 0; i < windowClients.length; i++) {
                  var client = windowClients[i];
                  // If so, just focus it.
                  if (client.url === url && "focus" in client) {
                    return client.focus();
                  }
                }
                // If not, then open the target URL in a new window/tab.
                if (clients.openWindow) {
                  return clients.openWindow(url);
                }
              })
            );
          });
          var notificationTitle = payload.data.title; //or payload.notification or whatever your payload is
          var notificationOptions = {
            body: payload.data.body,
            icon: "",
            data: { url: payload.data.click_action }, //the url which we gonna use later
            actions: [{ action: "open_url", title: "Read Now" }],
          };
          return self.registration.showNotification(
            notificationTitle,
            notificationOptions
          );
        });
      });
  };



registerfcmslistener=()=>{
navigator.serviceWorker
.register(“/static files/firebase messaging sw.js”)
。然后((注册)=>{
firebase.initializeApp(settings.getConfig().firebase\u CONFIG);
const messaging=firebase.messaging();
messaging.useServiceWorker(注册);
//请求权限并获取令牌。。。。。
试一试{
消息传递
.requestPermission()
.然后(()=>{
返回消息。getToken();
})
。然后((令牌)=>{
//让topic=`${userInfo.is_host?“host”:“guest”}`;
//本.订阅主题(主题、令牌);
这是sendTokenToServer({
操作系统:“网络”,
推送令牌:令牌,
});
});
}捕获(错误){
if(error.code==“消息传递/权限被阻止”){
console.log(“请手动解除阻止通知请求”);
}否则{
日志(“发生错误”,错误);
}
}
messaging.onMessage((有效负载)=>{
控制台日志(“ddd”,有效载荷);
self.addEventListener(“notificationclick”,函数(事件){
让url=”https://google.com/";
event.notification.close();//Android需要显式关闭。
event.waitill(
matchAll({type:“window”})。然后((windowClients)=>{
//检查是否已打开带有目标URL的窗口/选项卡
对于(var i=0;i
我有两个问题。首先,当我的网页打开时,我没有收到通知;第二,我想为通知设置onClick,以打开我所需的地址,但我没有取得任何进展。我会感谢你们的帮助