0

我在 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
4

1 回答 1

1

正如 Manoj Rejinthala 评论的那样,推送通知在浏览器中不起作用(ionic serve)。但是,旅游应用程序的其他部分应该在带有ionic serve.

您描述的错误是 Ionic Cloud 的问题。检查文件中是否有该行,"@ionic/cloud-angular":"^ 0.9.0"在.package.jsondependencies

如果是这样,将其切换到^0.9.1,保存文件并npm install在项目根文件夹中运行。这应该更新@ionic/cloud-angular0.9.1及其@ionic/cloud依赖于0.15.1.

然后运行ionic serve,它应该可以工作。

于 2017-01-25T14:15:53.447 回答