【问题标题】:TypeError Object is not a constructor (evaluating new_pubnubReact.default')TypeError 对象不是构造函数(评估 new_pubnubReact.default')
【发布时间】:2020-04-21 15:16:44
【问题描述】:

我是原生反应新手,正在尝试为 android 创建推送通知。

我正在使用 PubNub 的以下教程。

PubNub tutorial

当我完成教程后在 android studio 模拟器中运行我的应用程序时,我收到以下错误。

不太清楚如何解决它意味着什么,因为当我用谷歌搜索问题时没有出现。

这是我的代码

import React from 'react';
    import PushNotificationIOS from 'react-native';
    import PubNubReact from 'pubnub-react';

    const PushNotification = require('react-native-push-notification');

    export default class App extends React.Component {
    constructor(props) {
    super(props);
    this.pubnub = new PubNubReact({
        publishKey: 'YOUR_PUBNUB_PUBLISH_KEY_HERE',
        subscribeKey: 'YOUR_PUBNUB_SUBSCRIBE_KEY_HERE'
    });
    this.pubnub.init(this);
    PushNotification.configure({
      // Called when Token is generated.
      onRegister: function(token) {
          console.log( 'TOKEN:', token );
          if (token.os == "ios") {
            this.pubnub.push.addChannels(
            {
              channels: ['notifications'],
              device: token.token,
              pushGateway: 'apns'
            });
            // Send iOS Notification from debug console: {"pn_apns":{"aps":{"alert":"Hello World."}}}
          } else if (token.os == "android"){
            this.pubnub.push.addChannels(
            {
              channels: ['notifications'],
              device: token.token,
              pushGateway: 'gcm' // apns, gcm, mpns
            });
            // Send Android Notification from debug console: {"pn_gcm":{"data":{"message":"Hello World."}}}
          }  
      }.bind(this),
      // Something not working?
      // See: https://support.pubnub.com/support/solutions/articles/14000043605-how-can-i-troubleshoot-my-push-notification-issues-
      // Called when a remote or local notification is opened or received.
      onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );
        // Do something with the notification.
        // Required on iOS only (see fetchCompletionHandler docs: https://reactnative.dev/docs/pushnotificationios)
        // notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
      // ANDROID: GCM or FCM Sender ID
      senderID: "sender-id",
  });
}
    }

【问题讨论】:

  • 这篇文章能回答你的问题吗? stackoverflow.com/questions/61080350/…
  • 我已经准备好看到这篇文章并按照它说的做了,但遗憾的是它没有用
  • 感谢您的确认。但是你的问题和那个不同吗?我只需要知道我是否应该将 PubNub 工程师的注意力集中在一个或两个上。
  • 你使用的是什么版本的pubnub-react
  • @AreWojciechowski - 根据后面的教程,它应该是 pubnub-react@2.0.0

标签: react-native react-native-android pubnub


【解决方案1】:

pubnub-react 库在版本2.0.0 中已完全更改。它不再默认包含pubnub JavaScript SDK,因此您也必须安装它。

Here is the link 到新的 PubNub React 存储库,在 README.md 文件中,您可以找到有关如何使用它的示例。


如果您想使用与您可能正在阅读的教程/博客文章兼容的旧版本,请安装旧版本的 PubNub React SDK,如下所示:

$ npm install pubnub-react@1

总结这些变化,pubnub-react 现在使用 Context 和 Hooks API 将 PubNub 实例深入传播到子树中。

提供者

您需要将提供程序包含在组件树顶部的某个位置。

import React from 'react'
import PubNub from 'pubnub'
import { PubNubProvider } from 'pubnub-react'

const pubnub = new PubNub({}) // PubNub configuration

export const App = () => {
    return <PubNubProvider client={pubnub}>
        <Child />
    </PubNubProvider>
}

消费者

要在其他地方使用 PubNub 实例,您现在只需使用 usePubNub 挂钩。

import { usePubNub } from 'pubnub-react'

export const Child = () => {
    const pubnub = usePubNub()
    
    return <div>I am using PubNub!</div>
}

【讨论】:

  • 如果是这种情况,他们需要更新他们的文档
  • 那么,@becky,答案是否为您解决了问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多