草庐IT

javascript - Unhandled Promise rejection : push. on is not a function

coder 2025-01-20 原文

我正在使用 Ionic 2。

我在尝试设置推送通知时收到此 Typescrpt 错误。我从教程中复制了这个示例代码,所以我希望它能工作。我一定是出了什么问题。任何想法请:

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 在浏览器中不可用。

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

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

关于javascript - Unhandled Promise rejection : push. on is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346571/

有关javascript - Unhandled Promise rejection : push. on is not a function的更多相关文章

随机推荐