как настроить уведомление FCM для прогрессивного веб-приложения - PullRequest
0 голосов
/ 28 мая 2020

Я работаю над уведомлениями PWA pu sh. Я могу видеть уведомления с моего собственного сервера через FCM. Мое требование - показать уведомление таким образом, чтобы

Например: Title: Reporting Manager Alert!
body: Venkat подал заявку на Leave et c.

Для этого мой код на стороне сервера:


HttpPost postRequest = new HttpPost(
                    "https://fcm.googleapis.com/fcm/send");

// we already created this model class.
// we will convert this model class to json object using google gson library.

NotificationRequestModel notificationRequestModel = new NotificationRequestModel();
NotificationData notificationData = new NotificationData();

notificationData.setDetail("Reporting Manager Alert!");
notificationData.setTitle(empName+" "+" has applied for leave");
notificationData.setname(empName);
notificationData.setclick_action("https:xxxxxx.xxx/xxxxxx.html");
notificationData.seticon("firebase-logo.png");
notificationRequestModel.setData(notificationData);
notificationRequestModel.setTo
(empTokenNo.trim());


Gson gson = new Gson();
Type type = new TypeToken<NotificationRequestModel>() {
}.getType();

String json = gson.toJson(notificationRequestModel, type);

my firebase-messaging-sw. js


    const messaging = firebase.messaging();
    self.addEventListener("push", payload => {
        // alert("push payload ");
        console.log(payload+"payload");
            const notificationTitle = payload.data.title;       //payload.getData().getTitle(); 
          const notificationOptions = {
            body: payload.data.body,
            icon: '/firebase-logo.png'
          };

      return self.registration.showNotification(notificationTitle,
        notificationOptions);

            });

Я получаю титул : undefined
Также я пытался поместить код pu sh в файл service-worker. js. здесь код


    self.addEventListener("push", event => {
        console.log('[Service Worker] Push Received.');
        //console.log(event+"event");
        console.log('[Service Worker] Push had this data: ${event.data.text()}');

        const title = {event.data.text()};  //'Reporting Manager Alert';
        const options = {
            body: event.data.title,
            icon: 'images/icon.png',
            badge: 'images/badge.png'
        }
     event.waitUntil(self.registration.showNotification(title, options));

    });

но не повезло, пожалуйста, помогите, как показать мое желаемое уведомление. Заранее спасибо.

...