Skip to content

Redirect for external URL doesnt work when click in a push #6276

Closed
@guimap

Description

@guimap

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: Chrome 100.0.4896.127
  • Firebase SDK version: 8.10.1
  • Firebase Product: Messaging

[REQUIRED] Describe the problem

I'm trying make a redirect when a push notification is clicked, after some reasearch noticed that has a code into firebase-sdk which doesnt allow redirect for an external URL. There's any way to make this redirect when a push notification is clicked ?
I tried to overwrite the onnotificationclick event listener on my service worker file, but this code on firebase sdk https://github.com/firebase/firebase-js-sdk/blob/master/packages/messaging/src/listeners/sw-listeners.ts#L121
doesnt allow overwrite.

My Question, can i redirect the user when he click on push which has an external URL ?

Relevant Code:

// service worker.js

 importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js");
 importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-analytics.js');
 importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');
 var firebaseApp = firebase.initializeApp(response.firebase);



     class CustomPushEvent extends Event {
         constructor(data) {
             super('push')

             Object.assign(this, data)
             this.custom = true
         }
     }


     /*
  * Overrides push notification data, to avoid having 'notification' key and firebase blocking
  * the message handler from being called
  */
 self.addEventListener('push', (e) => {
     // Skip if event is our own custom event
     if (e.custom) return;

     // Kep old event data to override
     let oldData = e.data

     // Create a new event to dispatch, pull values from notification key and put it in data key,
     // and then remove notification key
     let newEvent = new CustomPushEvent({
         data: {
             json() {
                 let newData = oldData.json()
                 newData.data = {
                     ...newData.data,
                     ...newData.notification
                 }
                 delete newData.notification
                 return newData
             },
         },
         waitUntil: e.waitUntil.bind(e),
     })

     // Stop event propagation
     e.stopImmediatePropagation()

     // Dispatch the new wrapped event
     dispatchEvent(newEvent)
 })


      /*
      * Overrides push notification data, to avoid having 'notification' key and firebase blocking
      * the message handler from being called
      */
      console.log('Registering Event Listener...')
      var messaging = firebase.messaging();
      messaging.onBackgroundMessage(function (payload) {
          console.log('New Event received', payload)
          const notificationTitle = payload.data.title;
          const notificationOptions = {
              body: payload.data.body,
              icon: payload.data.icon,
              data: payload.data
          };


          self.registration.showNotification(notificationTitle,
              notificationOptions)

      });

      self.onnotificationclick = function (event) {
         //This event never happens
         console.log('Clicou')
     }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions