I'm trying to make push notifications work in Angular 7 using @angular/pwa link and using SwPush. I am not able to get actual push notifications. I am currently working on localhost (by running http-server after doing an ng-build) and my api server is located in cloud. I was able to enable the subscription using swPush.requestSubscription and the subscription is registered successfully on the server. In Chrome, all the api calls get blocked from the service worker itself (failed : from service Worker) while in Firefox, there is no error but the push message does not appear.
I have added the relevant code snippets below. Since there are no specific errors reported, I am not able to proceed further.
Please advise on how to make this work and show notifications.
app.module.ts
import {PushNotificationService} from 'core';
import { ServiceWorkerModule } from '@angular/service-worker';
@NgModule({
declarations: [
AppComponent,
],
imports: [
ServiceWorkerModule.register('ngsw-worker.js', { enabled: true })
],
providers: [
PushNotificationService,
],
exports: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
app.component.ts
export class AppComponent {
constructor(private pushNotification :PushNotificationService,
private swPush : SwPush){
this.swPush.messages.subscribe(notification => {
const notificationData: any = notification;
const options = {
body: notificationData.message,
badgeUrl: notificationData.badgeUrl,
icon: notificationData.iconUrl
};
navigator.serviceWorker.getRegistration().then(reg => {
console.log('showed notification');
reg.showNotification(notificationData.title, options).then(res => {
console.log(res);
}, err => {
console.error(err);
});
});
});
}
isSupported() {
return this.pushNotification.isSupported;
}
isSubscribed() {
console.log(' ****** profile component' + this.swPush.isEnabled);
return this.swPush.isEnabled;
}
enablePushMessages() {
console.log('Enable called');
this.pushNotification.subscribeToPush();
}
disablePushMessages(){
// code for unsubsribe
}
}
push.notification.service
export class PushNotificationService {
public isSupported = true;
public isSubscribed = false;
private swRegistration: any = null;
private userAgent = window.navigator.userAgent;
constructor(private http: HttpClient, private swPush: SwPush) {
if ((this.userAgent.indexOf('Edge') > -1) ||
(this.userAgent.indexOf('MSIE') > -1) || (this.userAgent.indexOf('.Net')
> -1)) {
this.isSupported = false;
}
}
subscribeToPush() {
// Requesting messaging service to subscribe current client (browser)
let publickey = 'xchbjhbidcidd'
this.swPush.requestSubscription({
serverPublicKey: publickey
}).then(pushSubscription => {
console.log('request push subscription ', pushSubscription);
this.createSubscriptionOnServer(pushSubscription);
})
.catch(err => {
console.error(err);
});
}
createSubscriptionOnServer(subscription) {
let urlName = 'api/user/notificationSubscription';
let params;
params = {
endpoint: subscription.endpoint,
};
this.http.put<any>(urlName, params, httpOptions).pipe(
tap((res) => {
if (res.data) {
if (res.data.success) {
alert('Success')
} else {
alert('error')
}
}
}));
}
}
from Angular 7 pwa/ SwPush - push notifications not working
No comments:
Post a Comment