Currently it's too hard read js for include onesignal and add custom overrides for onesignal scripts (like setExternalUserId, setEmail, setSMSNumber). This patch provide more clean js for include onesignal with ability add custom snippets.

Issue fork onesignal-3318255

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cosolom created an issue. See original summary.

cosolom’s picture

Status: Active » Needs review
geoanders’s picture

@cosolom

Thanks for this! I do like the cleaner and easier to read code.

I think we will need to add some information around the JS injected piece and update README. Also, does OneSignal recommend this or was this something you needed in your use case?

geoanders’s picture

Status: Needs review » Needs work

  • geoanders committed 134a2e5 on 3.x
    Issue #3318255 - Add in onesignal.api.php file and minor adjustments.
    
geoanders’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cosolom’s picture

@geoanders without this is too hard inject own external user id and other information. For example with that code I can do this in my own hook:

function cpl_onesignal_onesignal_extra_js() {
  $result = [];
  /** @var \Drupal\onesignal\Config\ConfigManager $config_manager */
  $config_manager = \Drupal::service('onesignal.config_manager');
  $rest_api_key = $config_manager->getRestApiKey();
  if (!$rest_api_key) {
    return '';
  }
  if ($uid = \Drupal::currentUser()->id()) {
    $uid_auth_hash = hash_hmac('sha256', $uid, $rest_api_key);
    $result[] = '
      var oneSignalExternalUserId  = "' . $uid . '";
      var oneSignalExternalUserIdAuthHash = "' . $uid_auth_hash . '";
      OneSignal.setExternalUserId(oneSignalExternalUserId, oneSignalExternalUserIdAuthHash);
    ';
  }
  return implode(PHP_EOL, $result);
}