从服务人员访问全局ZoomMtg对象

时间:2019-04-21 14:11:05

标签: javascript reactjs service web-worker

我想从我的服务工作者那里调用ZoomMtg API中的本机方法来做一些事情,这意味着根据用户的接受动作来打开一个缩放会议。我了解我无法从服务工作者访问全局缩放对象,因此必须使用postMessage API。任何帮助将不胜感激。因为我不知道该怎么办。

import { APP_KEY, APP_SECRET } from './Helpers/CONSTAT';

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

let fldMeetingID = null;

// Remove SDK push handler
self.removeEventListener('push', self.OneSignalWorker.onPushReceived);
// Register our own custom handler
self.addEventListener('push', (event) => {
  if (event.data) {
    const data = event.data.json();
    const notification = self.OneSignalWorker.buildStructuredNotificationObject(data);
    if (notification.data.isCanceled !== 1) {
      fldMeetingID = notification.data.fldMeetingID;
      const options = {
        body: notification.data.fldUser,
        badge: './assets/images/mipmap-hdpi/ic_launcher.png',
        vibrate: [200, 100, 200, 100, 200, 100, 200],
        tag: 'vibration-sample',
        sound: '',
        dir: 'auto',
        requireInteraction: 'true',
        actions: [
          {
            action: 'accept-action',
            title: 'ACCEPT'
          },
          {
            action: 'decline-action',
            title: 'DECLINE'
          }
        ]
      };
      return self.registration.showNotification('WaveLink Session Notification', options);
    } else {
      // DON'T SHOW ANYTHING
      return new Promise(function(resolve, reject) {});
    }
  }
});
// With this we would detect notification clicks or action clicks like so
self.addEventListener('notificationclick', (event) => {
  const clickedNotification = event.notification;
  if (!event.action) {
    // Was a normal notification click
    console.log('Notification Click.');
    return;
  }

  function connectToCurrentMeeting(fldMeetingID) {
    const meetConfig = {
      apiKey: APP_KEY,
      apiSecret: APP_SECRET,
      meetingNumber: fldMeetingID,
      leaveUrl: 'https://zoom.us',
      role: 0
    };
      // TODO: i watn to access ZoomMtg here

        ZoomMtg.generateSignature({
        meetingNumber: meetConfig.meetingNumber,
        apiKey: meetConfig.apiKey,
        apiSecret: meetConfig.apiSecret,
        role: meetConfig.role,
        success(res) {
            console.log('signature', res.result);
            ZoomMtg.init({
                leaveUrl: 'http://www.zoom.us',
                isSupportAV: true,
                success() {
                    ZoomMtg.join(
                        {
                            meetingNumber: meetConfig.meetingNumber,
                            userName: meetConfig.userName,
                            signature: res.result,
                            apiKey: meetConfig.apiKey,
                            success() {
                                $('#zmmtg-root').css({'display': 'block'});
                                console.log('join meeting success');
                            },
                            error(res) {
                                console.log(res);
                            }
                        }
                    );
                },
                error(res) {
                    console.log(res);
                }
            });
        }
    });
  }

  switch (event.action) {
    case 'accept-action':
      const promiseChain = connectToCurrentMeeting(fldMeetingID);
      event.waitUntil(promiseChain);
      console.log('User ❤️️\'s accept.');
      break;
    case 'decline-action':
      console.log('User ❤️️\'s decline.');
      clickedNotification.close();
      break;
    default:
      console.log(`Unknown action clicked: '${event.action}'`);
      break;
  }
});

0 个答案:

没有答案