BxDolPushOneSignal.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function BxDolPushOneSignal(oOptions) {
  2. this._sObjName = oOptions.sObjName == undefined ? 'oBxDolPushOneSignal' : oOptions.sObjName;
  3. this._sSiteName = oOptions.sSiteName == undefined ? '' : oOptions.sSiteName;
  4. this._aTags = oOptions.aTags == undefined ? {} : oOptions.aTags;
  5. this._sAppId = oOptions.sAppId == undefined ? '' : oOptions.sAppId;
  6. this._sShortName = oOptions.sShortName == undefined ? '' : oOptions.sShortName;
  7. this._sSafariWebId = oOptions.sSafariWebId == undefined ? '' : oOptions.sSafariWebId;
  8. this._sSubfolder = oOptions.sSubfolder == undefined ? '/plugins_public/onesignal/' : oOptions.sSubfolder;
  9. this._sNotificationUrl = oOptions.sNotificationUrl == undefined ? '' : oOptions.sNotificationUrl;
  10. this._sTxtNotificationRequest = oOptions.sTxtNotificationRequest == undefined ? 'Would you like to get notifications' : oOptions.sTxtNotificationRequest;
  11. this._sTxtNotificationRequestYes = oOptions.sTxtNotificationRequestYes == undefined ? 'Yes' : oOptions.sTxtNotificationRequestYes;
  12. this._sTxtNotificationRequestNo = oOptions.sTxtNotificationRequestNo == undefined ? 'No' : oOptions.sTxtNotificationRequestNo;
  13. var $this = this;
  14. $(document).ready(function() {
  15. $this.init();
  16. });
  17. }
  18. BxDolPushOneSignal.prototype.init = function() {
  19. var $this = this;
  20. window.OneSignal = window.OneSignal || [];
  21. OneSignal.push(function() {
  22. OneSignal.SERVICE_WORKER_UPDATER_PATH = "OneSignalSDKUpdaterWorker.js.php";
  23. OneSignal.SERVICE_WORKER_PATH = "OneSignalSDKWorker.js.php";
  24. OneSignal.SERVICE_WORKER_PARAM = { scope: '/' }; /* This registers the workers at the root scope, which is allowed by the HTTP header "Service-Worker-Allowed: /" */
  25. });
  26. OneSignal.push(["init", {
  27. appId: $this._sAppId,
  28. autoRegister: true, /* Set to true to automatically prompt visitors */
  29. subdomainName: $this._sShortName, /* required only for http sites */
  30. safari_web_id: $this._sSafariWebId,
  31. path: $this._sSubfolder, /* A trailing slash is required */
  32. persistNotification: false,
  33. welcomeNotification: {
  34. disable:false
  35. },
  36. httpPermissionRequest: {
  37. enable: true
  38. },
  39. promptOptions: {
  40. /* These prompt options values configure both the HTTP prompt and the HTTP popup. */
  41. /* actionMessage limited to 90 characters */
  42. actionMessage: $this._sTxtNotificationRequest,
  43. /* acceptButtonText limited to 15 characters */
  44. acceptButtonText: $this._sTxtNotificationRequestYes,
  45. /* cancelButtonText limited to 15 characters */
  46. cancelButtonText: $this._sTxtNotificationRequestNo
  47. }
  48. }]);
  49. OneSignal.push(function() {
  50. var isPushSupported = OneSignal.isPushNotificationsSupported();
  51. OneSignal.setDefaultNotificationUrl($this._sNotificationUrl);
  52. OneSignal.isPushNotificationsEnabled().then(function(isEnabled) {
  53. if('undefined' !== typeof($this._aTags['email'])) {
  54. OneSignal.setEmail($this._aTags['email'], $this._aTags['email_hash']);
  55. delete $this._aTags['email'];
  56. delete $this._aTags['email_hash'];
  57. }
  58. OneSignal.sendTags($this._aTags); // set tags for onesiganl, some of these tags will be used to send notification
  59. if(!isEnabled && isPushSupported)
  60. OneSignal.showHttpPermissionRequest();
  61. });
  62. });
  63. if(navigator.userAgent.indexOf('gonative') > -1) {
  64. var json = JSON.stringify($this._aTags);
  65. $(document).ready(function () {
  66. window.location.href='gonative://registration/send?customData=' + encodeURIComponent(json);
  67. });
  68. }
  69. };