This is a JavaScript module that can be used to easily include OneSignal code in a website that uses Angular for its front-end codebase.
OneSignal is the world's leader for Mobile Push Notifications, Web Push, and In-App Messaging. It is trusted by 2 million+ businesses to send 9 billion Push Notifications per day.
You can find more information on OneSignal here.
Upgrading from Version 1? See our migration guide to get started.
yarn add onesignal-ngx
npm install --save onesignal-ngx
import { OneSignal } from 'onesignal-ngx';
Initialize OneSignal with your appId
via the options
parameter:
@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.
Examples
await this.oneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
// do other stuff
this.oneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
// do other stuff
});
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).
Example:
this.oneSignal.init({
appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
serviceWorkerParam: {
scope: '<path scope>'
},
serviceWorkerPath: '<path>'
});
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.
This package includes Typescript support.
class OneSignal {
Slidedown: IOneSignalSlidedown;
Notifications: IOneSignalNotifications;
Session: IOneSignalSession;
User: IOneSignalUser;
Debug: IOneSignalDebug;
login(externalId: string, jwtToken?: string): Promise<void>;
logout(): Promise<void>;
init(options: IInitObject): Promise<void>;
setConsentGiven(consent: boolean): Promise<void>;
setConsentRequired(requiresConsent: boolean): Promise<void>;
}
See the official OneSignal WebSDK reference for information on all available SDK functions.
Use listeners to react to OneSignal-related events:
Event Name | Callback Argument Type |
---|---|
'click' | NotificationClickEvent |
'foregroundWillDisplay' | NotificationForegroundWillDisplayEvent |
'dismiss' | NotificationDismissEvent |
'permissionChange' | boolean |
'permissionPromptDisplay' | void |
Event Name | Callback Argument Type |
---|---|
'slidedownShown' | boolean |
Event Name | Callback Argument Type |
---|---|
'change' | boolean |
Example
this.oneSignal.Notifications.addEventListener('click', (event) => {
console.log("The notification was clicked!", event);
});
See the OneSignal WebSDK Reference for more info on the available event listeners.
serviceWorker
flagIn 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.
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Give a βοΈ if this project helped you!
Reach out to us via our Discord server!
Copyright Β© 2023 OneSignal.
This project is MIT licensed.
Enjoy!