I am using swPush in my angular project. In that, the event of this.swPush.requestSubscription() is getting called by clicking a button.
For Example:
this getdeviceToken will call
getDeviceToken(): Promise<any> {
console.log('getting device token')
return new Promise(async (resolve, reject) => {
if (this.swPush.isEnabled) {
console.log('before subscription')
let sub = await this.swPush.requestSubscription({
serverPublicKey: this.VAPID_PUBLIC_KEY
})
console.log('temp --------', sub)
if(sub) {
this.device_token = sub;
if(sub) {
console.log('if sub')
resolve(this.device_token);
}else{
console.log('else sub')
reject({ message: "Error getting device id"});
}
} else{
reject({ message: "Error getting device id"});
}
} else{
console.log('sw is not enabled');
reject({ message: "Error getting device id"});
}
}
After hard reload, when I click the button for the first time it is getting stuck on the
console.log('before subscription')
and returns nothing. It makes my app unusable since the function not returning anything not even an error. So I cannot move forward.
But when I do a simple reload and click the button again it works and this.swPush.requestSubscription() returns me a promise so my app is moving forward. Anyone know Why it is not working for the first time ? Or any suggestions to improve this.
I got this solution working, IDK how but it worked everytime...
Set push-notification permission to Ask(default) in the page
Ask for permission from user using
Notification.requestPermission().then( result => { getSubscriptionData(); });avoid using chrome as it checks for security, data coming from any server except https:// are blocked. Use updated EDGE or firefox instead
please comment out any other solution if you have... here's my full code: