Angular Setup

The Angular OneSignal Plugin is a JavaScript module that can be used to easily include OneSignal code in a website that uses Angular for its front-end codebase.

Requirements

If you have not done so already, you may benefit from following our Web Push Quickstart first.

Install

Yarn

yarn add onesignal-ngx

npm

npm install --save onesignal-ngx

Library setup

Initialize OneSignal with your appId via the options parameter:

import { OneSignal } from 'onesignal-ngx';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-example-app';

  constructor(private oneSignal: OneSignal) {
    this.oneSignal.init({
      appId: "8e7fe838-fbcd-4152-980d-32565a2dcf03",
    });
  }
}

The init function returns a promise that resolves when OneSignal is loaded.

//Example1
await this.oneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
// do other stuff

//Example2
this.oneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
  // do other stuff
});

Init Options

You can pass other options to the init function. Use these options to configure personalized prompt options, auto-resubscribe, and more (full list of initialization options).

Service Worker Params

You can customize the location and filenames of service worker assets. You are also able to specify the specific scope that your service worker should control. You can read more here.

In this distribution, you can specify the parameters via the following:

FieldDetails
serviceWorkerParamUse to specify the scope, or the path the service worker has control of. Example: { scope: "/js/push/onesignal/" }
serviceWorkerPathThe path to the service worker file.
this.oneSignal.init({
   appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
   serviceWorkerParam: {
     scope: '<path scope>'
   },
   serviceWorkerPath: '<path>'
});

Service Worker File

If you haven't done so already, you will need to add the OneSignal Service Worker file to your site (learn more).

The OneSignal SDK file must be publicly accessible. You can put it in your top-level root or a subdirectory. However, if you are placing the file not on top-level root make sure to specify the path via the service worker params in the init options (see section above).

Tip:
Visit https://yoursite.com/OneSignalSDKWorker.js in the address bar to make sure the file is being served successfully.

Troubleshooting:
If you uploaded the file but you cannot access it via your browser search bar, make sure you have told Angular about it via the assets param in your angular.json file.

293

OneSignal API

Typescript

This package includes Typescript support.

class OneSignal {
  init(options: IInitObject): Promise<void>;
  on(event: string, listener: Function): void;
  off(event: string, listener: Function): void;
  once(event: string, listener: Function): void;
  isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>;
  showHttpPrompt(options?: AutoPromptOptions): Promise<void>;
  registerForPushNotifications(options?: RegisterOptions): Promise<void>;
  setDefaultNotificationUrl(url: string): Promise<void>;
  setDefaultTitle(title: string): Promise<void>;
  getTags(callback?: Action<any>): Promise<void>;
  sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null>;
  sendTags(tags: TagsObject<any>, callback?: Action<Object>): Promise<Object | null>;
  deleteTag(tag: string): Promise<Array<string>>;
  deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>>;
  addListenerForNotificationOpened(callback?: Action<Notification>): Promise<void>;
  setSubscription(newSubscription: boolean): Promise<void>;
  showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any>;
  showNativePrompt(): Promise<void>;
  showSlidedownPrompt(options?: AutoPromptOptions): Promise<void>;
  showCategorySlidedown(options?: AutoPromptOptions): Promise<void>;
  showSmsSlidedown(options?: AutoPromptOptions): Promise<void>;
  showEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
  showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
  getNotificationPermission(onComplete?: Function): Promise<NotificationPermission>;
  getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null>;
  getSubscription(callback?: Action<boolean>): Promise<boolean>;
  setEmail(email: string, options?: SetEmailOptions): Promise<string | null>;
  setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise<string | null>;
  logoutEmail(): Promise<void>;
  logoutSMS(): Promise<void>;
  setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise<void>;
  removeExternalUserId(): Promise<void>;
  getExternalUserId(): Promise<string | undefined | null>;
  provideUserConsent(consent: boolean): Promise<void>;
  getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
  getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
  sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise<void>;
}

OneSignal API

See the official OneSignal WebSDK reference for information on all available SDK functions.


Advanced Usage

Events and Event Listeners

Use listeners to react to OneSignal-related events:

  • subscriptionChange
  • permissionPromptDisplay
  • notificationPermissionChange
  • popoverShown
  • customPromptClick
  • notificationDisplay
  • notificationDismiss
this.oneSignal.on('subscriptionChange', function(isSubscribed) {
  console.log("The user's subscription state is now:", isSubscribed);
});

See the OneSignal WebSDK Reference for more info on the available event listeners.

Troubleshooting

Service Worker Issues

Check the serviceWorker flag

In your angular.json, see if the serviceWorker flag is set to true. The flag is used to cause the production build to include some extra service worker files that will conflict with the OneSignal worker if they use the same scope. If your web app depends on this flag being true and hence the Angular service worker (ngsw-worker.js) like in PWA setups, you should customize your OneSignal service worker integration to use a different scope than the Angular service worker. Otherwise, they will conflict. This can be done using the service worker OneSignal initialization params documented above. Click for further details.