未处理的Promise拒绝:push.on不是一个函数

未处理的Promise拒绝:push.on不是一个函数

问题描述:

我正在使用Ionic 2.

I am using Ionic 2.

我在尝试设置推送通知时遇到此Typescrpt 错误。我已从教程中复制了此示例代码,因此可以预期它可以正常工作。我一定有点不对劲。任何想法请:

I get this Typescrpt error when trying to set up Push Notifications. I have copied this sample code from a tutorial, so would have expected it to work. I must have something wrong. Any ideas please:


Unhandled Promise rejection: push.on is not a function ; Zone: angular ; Task: Promise.then ; Value:
TypeError: push.on is not a function


push.on('registration', function (data) {


typescript

import { Push } from 'ionic-native';


  pushNotifications(): void {

    var push = Push.init({
      android: {
        vibrate: true,
        sound: true,
        senderID: "xxxxxxxxxxxxxxxxxxx"
      },
      ios: {
        alert: "true",
        badge: true,
        sound: 'false'
      },
      windows: {}
    });

    push.on('registration', (data) => {
      console.log(data.registrationId);
      alert(data.registrationId.toString());
    });
    push.on('notification', (data) => {
      console.log(data);
      alert("Hi, Am a push notification");
    });
    push.on('error', (e) => {
      console.log(e.message);
    });

  }


确保在使用插件之前检查'window.cordova'是否可用。你真的在设备或浏览器上测试吗? Cordova在浏览器中不可用。

Make sure to check if 'window.cordova' is available before using the plugin. Are you actually testing on a device or in browser? Cordova is not available within browser.

编辑
为了确保您的代码编辑器知道'window.cordova'是什么,请确保您安装了cordova类型。

EDIT To make sure your code editor knowns what 'window.cordova' is, make sure you installed cordova typings.

npm install typings -g
typings install dt~cordova --save --global