react-native - 将功能组件转换为类组件 React
问题描述
我刚刚开始学习 react native 大约一个月,只专注于课堂,但我发现我的推送通知(expo docs)是用 react hooks 编写的,我在边课上有一些访问问题,但我什么都不知道关于钩子....请帮我换课。谢谢大家。我尝试切换到课堂,但出现了很多错误 https://docs.expo.io/push-notifications/overview/
解决方案
对于 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 )
}
推荐阅读
- html - css2pdf xepOnline Cloudformatter - Hide elements in html but display in PDF
- swift - 如何将accessibilityIdentifier 设置为UIAlertActions?
- node.js - Json数组(动态键)+表格格式+angular5
- javascript - 在Extjs中的页面加载上隐藏文本字段上的光标
- javascript - JavaScript 匹配字符串数组的重音符号
- c# - Unity如何在预制件中关闭所有灯
- reactjs - 如何在 VS Code 中为 React Native 安装 Flow(代码建议)?
- php - 删除 WordPress 的附加图像大小后自动删除无用的附件元数据
- jquery - 我已经通过 jquery 插入了 linearGradient 标签 svg,但是 Gradient g 变小了我应该怎么做?
- actionscript-3 - 在 Datagride 中为动态列设置值