- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的“click_action”将用户带到我发送给客户端的通知上的特定 URL,但无论我做什么,要么什么都不做(桌面),要么只是打开 PWA(android)。消息通过正常(在 Chrome 控制台中检查)但单击似乎不起作用。
我的服务人员中有以下内容,这些内容来自各个地方,包括本网站上提供的其他答案:
importScripts('https://www.gstatic.com/firebasejs/7.14.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.3/firebase-messaging.js');
// importScripts('/__/firebase/init.js');
/* An empty service worker! */
self.addEventListener('fetch', function(event) {
/* An empty fetch handler! */
});
var firebaseConfig = {
//REDACTED
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
notificationTitle = payload.notification.title;
notificationOptions = {
body: payload.notification.body,
icon: payload.notification.icon,
click_action: payload.notification.click_action
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
self.addEventListener('notificationclick', function(event) {
let url = event.notification.click_action;
// I've also added a data.click_action field in my JSON notification, and have tried using that
// instead, but that didn't work either
console.log('On notification click: ', event.notification.tag);
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({ includeUncontrolled: true, 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);
}
})
);
});
self.onnotificationclick = function(event) {
let url = event.notification.click_action;
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == url && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow(url);
}));
};
{data: {…}, from: "xxx", priority: "high", notification: {…}, collapse_key: "do_not_collapse"}
collapse_key: "do_not_collapse"
data: {gcm.notification.badge: "[logo URL]", click_action: "https://[URL]/list.php?userid=33"}
from: "xxx"
notification:
body: "'5' has just been added"
click_action: "https://[URL]/list.php?userid=33"
icon: "https://[logo URL]"
title: "alert "
最佳答案
我花了很多时间为同样的问题寻找解决方案。也许这可以帮助:
如果您使用 firebase 消息发送通知,则可以使用 webpush
field 。 firebase 消息客户端库执行 self.registration.showNotification()
... 不再需要 messaging.onBackgroundMessage
在你的服务 worker 中。
// firebabse-coud-function.js
app.messaging().send({
webpush: {
notification: {
title: notification?.title || "Default title",
icon: notification?.icon || "/icon.png",
badge: notification?.icon || "/icon.png",
},
fcmOptions: {
link: `${BASE_URL || ""}${notification?.clickAction || "/"}`,
}
},
data: {
userID: notification.userID,
link: notification?.clickAction || "/",
},
topic
});
最重要的是,在你的 service worker 中添加一个 'notificationclick' 事件监听器
之前 调用
firebase.messaging()
所以我的服务人员看起来像:
// firebase-messaging-sw.js
// ...
self.addEventListener('notificationclick', function (event) {
console.debug('SW notification click event', event)
const url = event.notification?.data?.FCM_MSG?.data?.link;
// ...
})
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
// received others messages
})
对我来说,点击事件不会转到正确的网址。所以我添加这个:
// background client - service worker
const channel = new BroadcastChannel('sw-messages');
self.addEventListener('notificationclick', function (event) {
console.debug('SW notification click event', event)
const url = event.notification?.data?.FCM_MSG?.data?.link;
channel.postMessage({
type: 'notification_clicked',
data: {
title: event.notification.title,
clickAction: url
}
});
})
// foreground client
const channel = new BroadcastChannel('sw-messages');
channel.addEventListener("message", function (event) {
// go the page
})
我希望这可以帮助别人。
关于firebase-cloud-messaging - 无法让 click_action 使用 Web 应用程序/PWA 处理 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62204987/
我正在使用 Firebase Cloud Messaging 向我的应用的客户端发送通知。客户端可以是三个受支持平台(ios、android、web)中的任何一个。 我希望用户能够单击通知来启动应用程
我遇到了 FCM 的问题,正如 Firebase 团队记录的那样: click_action: Indicates the action associated with a user click on
我正在使用 Cloud Functions 并使用它发送推送通知。发送通知按预期工作,但 click_action 属性不起作用。当我点击通知时没有任何反应。这是我的代码: 我的通知负载: const
我有一个 Web 应用程序(Angular 6)和移动应用程序(Ionic 4),它们都连接到同一个数据库。我正在使用 FCM(Firebase Cloud Message)进行推送通知。我已经关注了
我的 fcm 点击操作有问题,收到通知后,当我点击它时,它应该打开浏览器,转到“www.google.co.in”,但是当我点击通知时,它会打开默认主要 Activity 。 这是我的 onMessa
我对 FCM 有疑问,当我收到通知时,我希望它打开特定 Activity ,默认情况下,当我不添加 click_action 时,它会打开应用程序的主要 Activity ,但是当我添加 click_
所以我目前正在开发 PWA。 我现在正在使用 推送通知 而且我已经能够使用以下非常简单的 JSON 结构接收后台和前台通知。 { "message":{ "token":"aValidTo
我的问题与( question )有些相似,但有一些不同。 我正在使用 FCM Admin SDK,但仍然没有看到为 Web 推送通知发送 click_action 属性的选项。 我也检查了 admi
这是我必须发送消息的代码。消息发送得很好,我的手机收到了远程通知;但是,该通知并不是我需要的可操作通知。 请帮忙!我需要有关 Swift 的任何建议或帮助。 我正在尝试发送上游消息。我希望此消息成为接
我实现了 Firebase 并测试了 Firebase 通知。当应用程序在前台时,我没有遇到任何问题,我实现了一项扩展 FirebaseMessagingService 并在 onMessageRec
当应用程序在后台时,当用户单击通知时,我试图打开一个特定的 Activity 。从文档中,我知道 click_action 必须添加到有效负载中,并在应用程序中添加一个 Intent 过滤器来处理它。
我正在使用这个 react-native-firebase 和 react-navigation 进行导航,我可以成功地从服务器或控制台推送通知,无论它是在前台还是后台,但我发誓文档不是很清楚如何打开
我试图让我的“click_action”将用户带到我发送给客户端的通知上的特定 URL,但无论我做什么,要么什么都不做(桌面),要么只是打开 PWA(android)。消息通过正常(在 Chrome
我是一名优秀的程序员,十分优秀!