Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 - No provider for Push

I was following this tutorial: https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59 trying to implement push notifications with Firebase, I think I did everything right but now when I run my app I get an exception: Error in :0:0 caused by: No provider for Push!

Any ideia how can I solve it?

There is my app.component.ts and app.module.ts

app.module.ts:

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
{Only App Pages ...}

@NgModule({
declarations: [
    MyApp,
    {Only App Pages ...}
],
imports: [
    IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
    MyApp,
    {Only App Pages...}
],
providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

App.component.ts:

import {Component} from "@angular/core";
import {AlertController, Nav, Platform} from "ionic-angular";
import {StatusBar} from "@ionic-native/status-bar";
import {SplashScreen} from "@ionic-native/splash-screen";
import {Push, PushObject, PushOptions} from "@ionic-native/push";
import { HomePage } from '../pages/home/home';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;

  constructor(public platform: Platform, 
                   public statusBar: StatusBar, 
                   public splashScreen: SplashScreen, 
                   public push: Push,
                   public alertCtrl: AlertController) {

    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
      this.initPushNotification();
    });
  }

  initPushNotification() {
    if (!this.platform.is('cordova')) {
      console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
      return;
    }
    const options: PushOptions = {
      android: {
        senderID: "883847118563"
      }
    };
    const pushObject: PushObject = this.push.init(options);

    pushObject.on('registration').subscribe((data: any) => {
      console.log("device token ->", data.registrationId);

      let alert = this.alertCtrl.create({
                  title: 'device token',
                  subTitle: data.registrationId,
                  buttons: ['OK']
                });
                alert.present();

    });

    pushObject.on('notification').subscribe((data: any) => {
      console.log('message', data.message);
      //if user using app and push notification comes
      if (data.additionalData.foreground) {
        // if application open, show popup
        let confirmAlert = this.alertCtrl.create({
          title: 'New Notification',
          message: data.message,
          buttons: [{
            text: 'Ignore',
            role: 'cancel'
          }, {
            text: 'View',
            handler: () => {
              //TODO: Your logic here
            //  this.nav.push(DetailsPage, {message: data.message});
            }
          }]
        });
        confirmAlert.present();
      } else {
        //if user NOT using app and push notification comes
        //TODO: Your logic on click of push notification directly
      //  this.nav.push(DetailsPage, {message: data.message})
      let alert = this.alertCtrl.create({
                  title: 'clicked on',
                  subTitle: "you clicked on the notification!",
                 buttons: ['OK']
                });
                alert.present();
        console.log("Push notification clicked");
      }
    });

    pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
  }
}

Thank you in advance for the help.

like image 745
João Silva Avatar asked Apr 12 '17 09:04

João Silva


1 Answers

You need to set Push as provider in app.module.ts

@NgModule({
declarations: [
    MyApp,
    {Only App Pages ...}
],
imports: [
    IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
    MyApp,
    {Only App Pages...}
],
providers: [
    StatusBar,
    SplashScreen,
    Push,//here
    {provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

In ionic native v3.x, all the plugins are set as providers and used by injecting in constructor.

like image 72
Suraj Rao Avatar answered Oct 08 '22 09:10

Suraj Rao