未处理的Promise拒绝:push.on不是一个函数 [英] Unhandled Promise rejection: push.on is not a function

查看:282
本文介绍了未处理的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

这篇关于未处理的Promise拒绝:push.on不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆