go to website from web notification firebase

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I build service that send web push notifications, with php and firebase. the notification works well, but I cant add link for send the user to another site. the request done with curl php. this event.data || event.data.notification always null.

self.addEventListener('notificationclick', function (event) {
    let nextUrl = event.notification.data.click_action; // ALWAYS NULL
    console.log('[Service Worker] Notification click Received.', nextUrl);
    event.notification.close();

    event.waitUntil(
        clients.openWindow(nextUrl)
    );
});

here is all the code.. firebase-messaging-sw.js

// [START initialize_firebase_in_sw]
importScripts('https://www.gstatic.com/firebasejs/5.5.9/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.5.9/firebase-messaging.js');

// messagingSenderId.
firebase.initializeApp({
    'messagingSenderId': '***'
});

// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

messaging.setBackgroundMessageHandler(function (payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);

    // Customize notification here
    const notificationTitle = payload.data.title;
    const notificationOptions = {
        body: payload.data.body,
        icon: payload.data.icon,
        image: payload.data.image,
        click_action: payload.data.nextLink
    };

    return self.registration.showNotification(notificationTitle,
        notificationOptions);
});
// [END background_handler]

self.addEventListener('notificationclick', function (event) {
    let nextUrl = event.notification.data.click_action; // ALWAYS NULL
    console.log('[Service Worker] Notification click Received.', nextUrl);
    event.notification.close();

    event.waitUntil(
        clients.openWindow(nextUrl)
    );
});

self.addEventListener('install', event => {
    self.skipWaiting();
});

main.js

// Initialize Firebase
var config 
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community