gpt4 book ai didi

ionic-framework - Ionic 2 - 没有推送的提供者

转载 作者:行者123 更新时间:2023-12-03 15:06:56 25 4
gpt4 key购买 nike

我正在关注本教程:https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59尝试使用 Firebase 实现推送通知,我认为我做的一切都是正确的,但现在当我运行我的应用程序时出现异常: :0:0 中的错误由以下原因引起:没有 Push 提供者!

任何想法我该如何解决?

有我的 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));
}
}

预先感谢您的帮助。

最佳答案

您需要设置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 {}

在 ionic native v3.x 中,所有插件都设置为提供程序,并通过注入(inject)构造函数来使用。

关于ionic-framework - Ionic 2 - 没有推送的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43365396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com