Firebase 推送通知点击不起作用 [英] Firebase push notifications click does not work

查看:83
本文介绍了Firebase 推送通知点击不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 firebase 实现通知时遇到问题.单击事件不起作用.我正在使用 HTTP 1 版本发送不记名令牌.

I am having problems to implement notifications using firebase. The click event does not work. I am using the HTTP 1 version sending the bearer token.

{
  "message": {
    "token": "8888****usertoken****8888",
    "notification": {
      "title": "Background Message Title",
      "body": "Background message body"
    },
    "webpush": {
      "fcm_options": {
        "link": "https://dummypage.com"
      }
    }
  }
}

我还尝试了 click_action、action 和许多其他变体,但都不起作用.

I have also tried click_action, action, and many other variations that just did not work.

我使用的是 8.0.0 版

I am using version 8.0.0

根据此链接上的文档https://firebase.google.com/docs/cloud-messaging/js/send-multiple,我应该可以使用 fcm_options 来实现它.

According to the documentation found on this link https://firebase.google.com/docs/cloud-messaging/js/send-multiple, I should be able to implement it using fcm_options.

我尝试了一个实现messages.onBackgroundMessage的解决方法,但是当我实现这个方法并使用self.registration.showNotification时,通知会显示两次.一个由浏览触发,另一个由此代码触发.

I tried a workaround implementing messaging.onBackgroundMessage, but when I implement this method and use self.registration.showNotification, the notification is displayed twice. one triggered by the browse and the other by this code.

注册 self.addEventListener('notificationclick' 似乎只有在我实现 onBackgroundMessage 时才有效.

Registering self.addEventListener('notificationclick' only seems to work when I implement onBackgroundMessage.

我遵循了文档,但它让我发疯.

I followed the documentation, but it is driving me crazy.

这是我的服务工作者代码:

This is my service worker code:

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

var firebaseConfig = {
    apiKey: "xxxxxx",
    authDomain: "xxxxxxxx.firebaseapp.com",
    databaseURL: "https://xxxxxx.firebaseio.com",
    projectId: "xxx-xxx",
    storageBucket: "xxx-xxx.appspot.com",
    messagingSenderId: "222222222",
    appId: "1:2222:web:22222"
};
console.log("fire base messaging")

firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();


messaging.onBackgroundMessage(function (payload) {
    console.log("onBackgroundMessage", payload)
    var dataFromServer = payload.notification;
    var notificationTitle = dataFromServer.title;
    var notificationOptions = {
        body: dataFromServer.body,
        image: dataFromServer.image,
        data: {
            url: "https://google.com"
        }
    };
    return self.registration.showNotification(notificationTitle,
        notificationOptions);
});

////Code for adding event on click of notification
self.addEventListener('notificationclick', function (event) {
    console.log("notificationclick", event)
    var urlToRedirect = event.notification.data.url;
    event.notification.close();
    event.waitUntil(self.clients.openWindow(urlToRedirect));
});

推荐答案

原来我将整个 URL 传递给 webpush.fcm_options.link = "https://google.com", 我所要做的就是只传递像 webpush.fcm_options.link = "/mypage" 这样的相对路径.

Turns out I was passing an entire URL to webpush.fcm_options.link = "https://google.com", all I had to do was to pass only the relative path like webpush.fcm_options.link = "/mypage".

所以发送请求应该是这样的:

So the request to send would be like this:

{
  "message": {
    "token": "8888****usertoken****8888",
    "notification": {
      "title": "Background Message Title",
      "body": "Background message body"
    },
    "webpush": {
      "fcm_options": {
        "link": "/mypage" 
      }
    }
  }
}

我在文档中没有看到说它只是相对路径.它甚至声明需要 HTTPS.我花了几个小时在这个上,我希望它可以帮助其他人.

I don't see in the docs say it is only the relative path. It even states that HTTPS is required. I spent a few hours on this one, I hope it helps somebody else.

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#WebpushFcmOptions

这篇关于Firebase 推送通知点击不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆