I'm in the process of incorporating OneSignal into my Angular 2 application for push notifications. Initially, I successfully implemented a HelloWorld app using basic HTML. However, when attempting to integrate it into my Angular app, users are not being created or registered, and therefore cannot receive any notifications.
Highlighted Code:
index.html
<html>
<head>
<meta charset="utf-8">
<title>My Angular App</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<head>
<link rel="manifest" href="/manifest.json">
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script>
var OneSignal = window.OneSignal || [];
console.log("Initializing OneSignal");
OneSignal.push(["init", {
appId: "xxx-xxx-xxx-xxx-xxx",
autoRegister: false,
allowLocalhostAsSecureOrigin: true,
notifyButton: {
enable: true
}
}]);
console.log('OneSignal Initialized');
OneSignal.push(function () {
console.log('Registered For Push');
OneSignal.getUserId().then(function (userId) {
console.log("User ID is", userId);
});
});
</script>
</head>
</head>
<body>
<app-root>
<div class="wrap">
<div class="loading outer">
<div class="loading inner"></div>
</div>
</div>
</app-root>
</body>
</html>
The user ID consistently returns as null.
I have verified the following:
- App ID is accurate
- Browser notification permissions are set to allow
Queries:
- Is there a method to execute all initialization processes inside a component, such as within the ngOnInit() method?
- How can one register for push notifications when a user clicks on a button within my component rather than utilizing the bell icon? (Disabling the notifyButton option will hide the bell icon)
P.S: Tested on Chrome using Angular CLI (Did not function with Firefox or Safari)