未处理的承诺拒绝:推.On不是一个函数

Unhandled Promise rejection: push.on is not a function

本文关键字:函数 一个 承诺 拒绝 On 未处理      更新时间:2023-09-26

我用的是Ionic 2。

当尝试设置推送通知时,我得到这个typescript 错误。我从教程中复制了这个示例代码,所以我希望它能正常工作。我一定是出了什么问题。有什么想法吗?
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) {

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);
    });
  }

一定要检查一下。在使用插件之前,Cordova '是可用的。你是在设备上还是在浏览器上进行测试?浏览器中无法使用Cordova。

编辑以确保您的代码编辑器知道什么窗口。确保你安装了Cordova类型。

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