Reputation: 27
I need to have push notifications on a Ionic (with Angular) app, I try to install LocalNotification but I can't achieve this, and there is no "tutorial" on how to install it.
Here is the git of LocalNotifications : https://github.com/katzer/cordova-plugin-local-notifications
I install it with npm and then I try to use it like that :
export class PushService {
constructor(private localNotifications: LocalNotifications) {}
public testNotif() {
// Schedule delayed notification
this.localNotifications.schedule({
text: 'Delayed ILocalNotification',
led: 'FF0000',
sound: null
});
}
}
But I always have this error : NullInjectorError: No provider for LocalNotifications!
The problem is that when I add LocalNotifications as a provider in my app.module then I have a new error :
Type 'LocalNotificationsOriginal' is not assignable to type 'Provider'.
What is wrong ? How to use it ?
Upvotes: 1
Views: 1348
Reputation: 770
Just in case anyone encounters the same problem. From the documentation
To use a plugin, import and add the plugin provider to your @NgModule, and then inject it where you wish to use it. Make sure to import the injectable class from the /ngx directory as shown in the following examples:
// app.module.ts
import { Camera } from '@ionic-native/camera/ngx';
...
@NgModule({
...
providers: [
...
Camera
...
]
...
})
export class AppModule { }
The key takeaway here, it to add to providers in app.module.ts and not other modules where you might want to use it. In components just inject as per usual into the constructor() {}
Have tried this with localNotifications and it works.
Upvotes: 4