pwatter icon indicating copy to clipboard operation
pwatter copied to clipboard

Uncaught (in promise): Error: StaticInjectorError[SwPush]:

Open dineshvasan opened this issue 6 years ago • 5 comments

Wow Awesome Bro for sharing your knowledge in Angular 5 using PWA. I have issue in SwPush and i have attached screen shot,kindly find the attachment Can you share me the simple example of source code while click the button option it will show the notification using swPush in Angular 5 with easy example. Kindly do the needful Bro.

This is my Mail ID [email protected]

swangular5 - google chrome

dineshvasan avatar Nov 14 '17 17:11 dineshvasan

Hello, did you register the ServiceWorkerModule in app.module? are you running the app in dev or prod mode? if I'm not mistaken, this problem happens because you're running in dev mode

marlonwanger avatar Nov 16 '17 16:11 marlonwanger

How to run the app in dev mode? Yes Service Worker is working fine on Prod mode and i need simple example of Push Notification without hitting on server. i got error on dev mode in the above image

Source Code

export class PushNotificationComponent implements OnInit {

private VAPID_PUBLIC_KEY: string; constructor(private swPush: SwPush,private configService: GlobalconfigService) { }

ngOnInit() { this.VAPID_PUBLIC_KEY = this.configService.get('VAPID_PUBLIC_KEY'); } tweets=[]; message: any ={"notification":{"title":"Ranjeet Kumar","actions":[{"action":"opentweet","title":"Open tweet"}],"body":"The latest The Top Javascript Blogs Daily! https://t.co/o3PSNkk9Di Thanks to @LifeWithKathy #makeyourownlane","dir":"auto","icon":"https://pbs.twimg.com/profile_images/854195961085734917/0X7AFONJ_normal.jpg","badge":"https://pbs.twimg.com/profile_images/854195961085734917/0X7AFONJ_normal.jpg","lang":"en","renotify":true,"requireInteraction":true,"tag":926796012340920300,"vibrate":[300,100,400],"data":{"url":"https://twitter.com/statuses/926796012340920321","created_at":"Sat Nov 04 12:59:23 +0000 2017","favorite_count":0,"retweet_count":0}}};

showPopupNotification(){

this.swPush.requestSubscription({
  serverPublicKey: this.VAPID_PUBLIC_KEY
})
.then(pushSubscription => {
  let notification = this.message['notification'];

  let body = {
    action: 'subscribe',
    subscription: pushSubscription
  }



})
.catch(err => {
  console.error(err);
})

}

dineshvasan avatar Nov 17 '17 10:11 dineshvasan

use @Optional decorator upon injection

https://angular.io/api/core/Optional

example:

import {Optional} from '@angular/core';

inside constructor: `

@Optional private swPush: SwPush

`,

Hope this will help

kangw3n avatar Nov 29 '17 02:11 kangw3n

Bro can u share the simple example of Push Notification without hitting on server. this so urgent . Kindly do the needful

dineshvasan avatar Nov 29 '17 08:11 dineshvasan

Use Injector

if (environment.production) {
      const swpush = this.injector.get(SwPush);
      // code use swpush
    }

allfayn avatar Nov 30 '17 12:11 allfayn