【问题标题】:Unhandled Promise rejection: push.on is not a function未处理的 Promise 拒绝:push.on 不是函数
【发布时间】:2016-09-08 23:50:39
【问题描述】:

我正在使用 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) {

打字稿

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

  }

【问题讨论】:

  • 尝试了以下相同的错误:push.on('registration', function (data) { console.log('++++++++++++++++++++++ FIRE ON!!'); alert(data); });
  • 很高兴我不是唯一一个。您对此有任何解决方案吗?
  • 还没有。与此同时,我已经开始着手其他工作了。

标签: javascript angular typescript ionic-framework ionic2


【解决方案1】:

确保在使用插件之前检查“window.cordova”是否可用。您实际上是在设备上还是在浏览器中进行测试? Cordova 在浏览器中不可用。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2018-12-09
    • 2021-10-14
    • 2020-11-16
    • 2020-05-06
    相关资源
    最近更新 更多