webmaxru / pwatter

Angular Progressive Web App using Workbox
https://workbox-workshop.firebaseapp.com
162 stars 30 forks source link

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

Open dineshvasan opened 6 years ago

dineshvasan commented 6 years ago

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 dineshvasanmca@gmail.com

swangular5 - google chrome

marlonwanger commented 6 years ago

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

dineshvasan commented 6 years ago

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);
})

}

kangw3n commented 6 years ago

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

dineshvasan commented 6 years ago

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

allfayn commented 6 years ago

Use Injector

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