Il semble que vous devriez ajouter l'événement notificationclick
en haut ** de firebase-messaging-sw.js
.
public/firebase-messaging-sw.js
// Notification click
self.addEventListener('notificationclick', function(event) {
let url = event.notification.data.FCM_MSG.data.url;
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 (let i = 0; i < windowClients.length; i++) {
let 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);
}
})
);
});
Lorsque je travaille avec des onglets normalement ouverts, je ne reçois pas de notifications push Web.
Vous pouvez définir la fonction onMessage
dans firebase.js
comme suit.
firebase.js
// Handle incoming messages while focusing
messaging.onMessage(function(payload) {
const { title, body, icon } = payload.notification
const url = payload.data.url
navigator.serviceWorker.getRegistration('/firebase-cloud-messaging-push-scope').then(registration => {
registration.showNotification(
title,
{
body,
icon,
data: {
FCM_MSG: {
data: {
url
}
}
}
}
)
});
});
Les paramètres qui peuvent être passés sont: title: Titre de la notification body: contenu de la notification icône: icône d'affichage click_action: URL qui transite avec un événement de clic (HTTPS uniquement)
app/jobs/push_notification_job.rb
class PushNotificationJob < ApplicationJob
queue_as :default
DEFAULT_ICON = 'https://hogehoge.ico'
def perform(token, options = {})
fcm = FCM.new(ENV['FIREBASE_SERVER_KEY'])
options = {
priority: 'high',
notification: {
title: options[:title],
body: options[:body],
icon: options[:icon] || DEFAULT_ICON,
},
data: {
url: options[:url] || '/'
}
}
registration_ids = [token]
fcm.send(registration_ids, options) if registration_ids.any?
end
end
Je ne sais toujours pas si priority: 'high'
est utile, mais ...
Recommended Posts