2017-04-12 73 views
2

我正在關注本教程:https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59試圖用Firebase實現推送通知,我認爲我做了一切正確的事情,但現在當我運行我的應用程序時出現異常:錯誤:0:0造成的:沒有提供者推!Ionic 2 - 沒有提供推送服務

任何意識如何解決它?

還有就是我app.component.ts和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)); 
    } 
} 

非常感謝您的幫助。

回答

17

您需要設置Push作爲提供商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 {} 

離子本地3.x版,所有的插件被設置爲供應商,並通過在構造函數中使用注入。

+0

謝謝!它的工作... 我已經搜索了這麼多,從未發現有什麼問題......再次感謝您 –

+0

感謝它的工作... – user320676