TypeError 对象不是构造函数(评估 new_pubnubReact.default') [英] TypeError Object is not a constructor (evaluating new_pubnubReact.default')

查看:64
本文介绍了TypeError 对象不是构造函数(评估 new_pubnubReact.default')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是本机反应的新手,正在尝试为 android 创建推送通知.

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

不太确定如何修复它意味着什么,因为当我在谷歌上搜索问题时什么也没有出现.

这是我的代码

从'react'导入React;从'react-native'导入PushNotificationIOS;从 'pubnub-react' 导入 PubNubReact;const PushNotification = require('react-native-push-notification');导出默认类 App 扩展 React.Component {构造函数(道具){超级(道具);this.pubnub = 新的 PubNubReact({发布密钥:'YOUR_PUBNUB_PUBLISH_KEY_HERE',subscribeKey: 'YOUR_PUBNUB_SUBSCRIBE_KEY_HERE'});this.pubnub.init(this);PushNotification.configure({//在生成 Token 时调用.onRegister:函数(令牌){控制台日志('令牌:',令牌);如果(令牌.os ==ios"){this.pubnub.push.addChannels({渠道:['通知'],设备:token.token,pushGateway:'apns'});//从调试控制台发送 iOS 通知:{"pn_apns":{"aps":{"alert":"Hello World."}}}} else if (token.os == "android"){this.pubnub.push.addChannels({渠道:['通知'],设备:token.token,pushGateway: 'gcm'//apns, gcm, mpns});//从调试控制台发送 Android 通知:{"pn_gcm":{"data":{"message":"Hello World."}}}}}.bind(this),//有些东西不起作用?//参见:https://support.pubnub.com/support/solutions/articles/14000043605-how-can-i-troubleshoot-my-push-notification-issues-//打开或接收到远程或本地通知时调用.onNotification:函数(通知){控制台日志('通知:',通知);//对通知做一些事情.//仅在 iOS 上需要(参见 fetchCompletionHandler 文档:https://reactnative.dev/docs/pushnotificationios)//通知.finish(PushNotificationIOS.FetchResult.NoData);},//ANDROID:GCM 或 FCM 发件人 ID发件人 ID: "发件人 ID",});}}

解决方案

pubnub-react 库在 2.0.0 版本中已经完全改变.默认情况下,它不再包含 pubnub JavaScript SDK,因此您也必须安装它.

这是新 PubNub React 存储库的链接,在 README.md 文件,您可以找到有关如何使用它的示例.


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

$ npm install pubnub-react@1


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

供应商

您需要在组件树的顶部包含提供程序.

从 'react' 导入 React从 'pubnub' 导入 PubNub从 'pubnub-react' 导入 { PubNubProvider }const pubnub 了了了 new PubNub({})//pubnub 配置export const App = () =>{return <孩子/></PubNubProvider>}

消费者

要在其他地方使用 PubNub 实例,您现在只需使用 usePubNub 钩子即可.

import { usePubNub } from 'pubnub-react'export const Child = () =>{const pubnub = usePubNub()返回 <div>我正在使用 PubNub!</div>}

I am new to react native and am trying to create push notifications for android.

I am using the following tutorial from PubNub.

PubNub tutorial

When I run my app in the android studio emulator after finishing the tutorial I get the following error.

Not quite sure what it means of how to fix it as when I google the problem nothing comes up.

Here is my code

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

解决方案

pubnub-react library has been completely changed in version 2.0.0. It no longers includes pubnub JavaScript SDK by default so you have to install it as well.

Here is the link to new PubNub React repository, and in the README.md file you can find examples on how to use it.


If you want to use the older version that is compatible with the tutorial/blog post you may be reading, please install the older version of the PubNub React SDK like so:

$ npm install pubnub-react@1


To summarize the changes, pubnub-react now uses Context and Hooks API to propagate PubNub instance deep into the children tree.

Provider

You need to include the provider somewhere top in the component tree.

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

Consumer

To use the PubNub instance somewhere else, you can now just use the usePubNub hook.

import { usePubNub } from 'pubnub-react'

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

这篇关于TypeError 对象不是构造函数(评估 new_pubnubReact.default')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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