Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Created August 8, 2018 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Antoinebr/cc68a61ec801fe7906b8051bdfe24c1b to your computer and use it in GitHub Desktop.
Save Antoinebr/cc68a61ec801fe7906b8051bdfe24c1b to your computer and use it in GitHub Desktop.
OneSIgnal custom UI

One Signal

Create a custom subscription button

function subscribe() {
  OneSignal.push(["registerForPushNotifications"]);
  event.preventDefault();
}

var OneSignal = OneSignal || [];
/* This example assumes you've already initialized OneSignal */
OneSignal.push(function() {
  // If we're on an unsupported browser, do nothing
  if (!OneSignal.isPushNotificationsSupported()) return;
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {

      if (isEnabled) {
          // The user is subscribed to notifications
          // Don't show anything
      } else {
          document.getElementById("subscribe-link").addEventListener('click', subscribe);
          document.getElementById("subscribe-link").style.display = '';
      }
  });
});

Change the Onesignal setting to disable the default subscription mechanism

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
var OneSignal = window.OneSignal || [];
    OneSignal.push(function() {
        OneSignal.init({
            appId: "dca7ff36-aca9-4c51-aaf8-21a559216225",
            autoRegister: false,
            notifyButton: {
                enable: false, // Here we disable 
            },
        });
    });
</script>

Send specific notifications

You maybe want to send target notifications, you can do it through tags.

Subscribe a user to a specific tag

OneSignal.sendtag('bitcoin','+50') 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment