4

My service worker file has the following code and I am sending CURL request which has data and notification key value is inside data:

curl -X POST -H "Authorization: key=MY_KEY" -H "Content-Type: application/json" -d '{
  "data": {
    "image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg",
    "notification": {
      "title": "Sample Title",
      "body": "Sample Body",
      "icon": "https://abcd.com/web-push-logo.png",
      "click_action": "https://google.com",
      "image": "https://abcd.com/article/4620/pictures/3336517/1.jpg"
    }
  }
  "to": "<REGISTRATION_ID>"
}' "https://fcm.googleapis.com/fcm/send"

Inside Service Worker file:

  messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  res = JSON.parse(payload.data.notification)
  var notificationTitle = res.title;
  var notificationOptions = {
    click_action: "https://google.com",
    body: res.body,
    icon: res.icon,
    image: res.image
  };
  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});

Click action doesn't work when I put notification inside data object and work if I send notification outside data.

Talha Shoaib
  • 111
  • 5
  • **From the [firebase docs](https://firebase.google.com/docs/cloud-messaging/js/receive#setting_notification_options_in_the_service_worker):** Because data messages don't support click_action, you are recommended to add a notification payload to all data messages. Alternatively, you can handle notifications using the service worker. – DragonBorn Nov 30 '18 at 07:01

0 Answers0