ionic serve - Runtime Error : Push plugin not found (IONIC 2)
ionic serve - Runtime Error : Push plugin not found (IONIC 2)
我使用以下代码在 ionic 2 应用程序中使用推送通知。
import { Push, PushToken } from '@ionic/cloud-angular';
@Component({...})
export MyPage {
constructor(public platform: Platform, public menu: MenuController, public push: Push){
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
if (this.push) {
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
window.localStorage.setItem("deviceToken", t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
console.log('notification msg', msg);
});
}
}
}
}
当我 运行 在设备上时,它工作正常。但我做离子服务它给出了以下错误,因为在构造函数
中注入了Push
error_handler.js:53 TypeError: platform.toLowerCase is not a function
at Insights.normalizeDevicePlatform (http://localhost:8100/build/main.js:70460:25)
at Insights.markActive (http://localhost:8100/build/main.js:70450:33)
at Insights.checkActivity (http://localhost:8100/build/main.js:70439:22)
at http://localhost:8100/build/main.js:70415:27
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9723)
at Object.onInvokeTask (http://localhost:8100/build/main.js:41825:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9659)
at e.runTask (http://localhost:8100/build/polyfills.js:3:7083)
at invoke (http://localhost:8100/build/polyfills.js:3:10836)
at e.args.(anonymous function) (http://localhost:8100/build/polyfills.js:2:30123)
ErrorHandler.handleError @ error_handler.js:53
任何帮助都适用
您应该在执行cordova 操作之前检查cordova 对象是否存在。
服务中不存在,因为浏览器不是移动设备
initializeApp() {
this.platform.ready().then(() => {
if(!(<any>window).cordova) return;
...the native code you want to execute...
});
}
检查 package.json
文件中 dependencies
.
中是否有行 "@ionic/cloud-angular":"^ 0.9.0"
如果是,将其切换为^0.9.1
,将文件和运行 npm install
保存在项目根文件夹中。这应该将 @ionic/cloud-angular
更新为 0.9.1
并将其 @ionic/cloud
依赖项更新为 0.15.1
.
然后 运行 ionic serve
它应该可以工作。
我使用以下代码在 ionic 2 应用程序中使用推送通知。
import { Push, PushToken } from '@ionic/cloud-angular';
@Component({...})
export MyPage {
constructor(public platform: Platform, public menu: MenuController, public push: Push){
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
if (this.push) {
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
window.localStorage.setItem("deviceToken", t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
console.log('notification msg', msg);
});
}
}
}
}
当我 运行 在设备上时,它工作正常。但我做离子服务它给出了以下错误,因为在构造函数
中注入了Pusherror_handler.js:53 TypeError: platform.toLowerCase is not a function
at Insights.normalizeDevicePlatform (http://localhost:8100/build/main.js:70460:25)
at Insights.markActive (http://localhost:8100/build/main.js:70450:33)
at Insights.checkActivity (http://localhost:8100/build/main.js:70439:22)
at http://localhost:8100/build/main.js:70415:27
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9723)
at Object.onInvokeTask (http://localhost:8100/build/main.js:41825:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9659)
at e.runTask (http://localhost:8100/build/polyfills.js:3:7083)
at invoke (http://localhost:8100/build/polyfills.js:3:10836)
at e.args.(anonymous function) (http://localhost:8100/build/polyfills.js:2:30123)
ErrorHandler.handleError @ error_handler.js:53
任何帮助都适用
您应该在执行cordova 操作之前检查cordova 对象是否存在。
服务中不存在,因为浏览器不是移动设备
initializeApp() {
this.platform.ready().then(() => {
if(!(<any>window).cordova) return;
...the native code you want to execute...
});
}
检查 package.json
文件中 dependencies
.
"@ionic/cloud-angular":"^ 0.9.0"
如果是,将其切换为^0.9.1
,将文件和运行 npm install
保存在项目根文件夹中。这应该将 @ionic/cloud-angular
更新为 0.9.1
并将其 @ionic/cloud
依赖项更新为 0.15.1
.
然后 运行 ionic serve
它应该可以工作。