将功能组件转换为类组件 React

时间:2021-01-01 02:32:56

标签: react-native

我刚开始学习 React Native 大约一个月,只专注于课堂,但我发现我的推送通知(博览会文档)是用 React 钩子编写的,我在副课上有一些访问问题,但我没有我对钩子一无所知……请帮我换班。谢谢大家。我尝试切换到课堂,但遇到了很多错误 https://docs.expo.io/push-notifications/overview/

1 个答案:

答案 0 :(得分:1)

对于exp推送通知(类类型组件),请使用以下内容:

import React, {Component}  from 'react';
//import { Notifications } from 'expo';
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import Constants from 'expo-constants';

import {
    Alert,
    Vibration, Platform,
    Image,
  StyleSheet,
  View,
  AsyncStorage,
  TextInput,
  Text,
  TouchableOpacity,
} from 'react-native';

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});


export default class xxxxxx extends Component {

 state = {
    expoPushToken: '',
    notification: {},
  };

  registerForPushNotificationsAsync = async () => {
   
      let token;
    
      if (Constants.isDevice) {
    const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = existingStatus;
    if (existingStatus !== 'granted') {
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;
    }
    if (finalStatus !== 'granted') {
      alert('Failed to get push token for push notification!');
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;

      
//this.setState({
//      xbuttonstate: false
//    })    

    
     this.setState({expoPushToken:token})

// alert(this.state.expoPushToken);
// unremark the above if you want to see the expoPushToken

  } else {
    alert('Must use physical device for Push Notifications');
  }
    
    
    
    
  if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }
    
    
    
  };

/// Add other codes you want (e.g. render /view )

}