import React, { Component } from 'react'; import { View, Text, StyleSheet, Clipboard, Platform } from 'react-native'; import Button from '../../components/Button'; import PushNotification from "react-native-push-notification"; import Dialog from '../../components/Dialog'; import apiUpload from '../../api/apiUpload'; export default class Notify extends Component { constructor(props) { super(props); this.state = { permission: false, notifyToken: 'none' }; } componentDidMount() { //this.requestUserPermission() if (global.notifyToken.token) { this.setState({ notifyToken: global.notifyToken.token }) } } async requestUserPermission() { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; this.setState({ permission: enabled }) if (enabled) { console.log('Authorization status:', authStatus); } this.getToken() } getToken() { messaging().getToken().then(token => { if (token) { this.setState({ notifyToken: token }) console.log("token", token); } }).catch(err => { console.info('Token Error', err) }); } localNotification() { PushNotification.localNotification({ /* Android Only Properties */ channelId: "10186", // (required) channelId, if the channel doesn't exist, notification will not trigger. showWhen: true, // (optional) default: true autoCancel: true, // (optional) default: true title: "JuicePlus", // (optional) default: "message" prop message: "This is a local test message", // (optional) default: none vibrate: true, // (optional) 振动开关default: true vibration: 300, // 振动长度(毫秒),如果vibrate=false,则忽略, default: 1000 invokeApp: true, // 此选项允许单击操作将应用程序带回前台或留在后台, default: true smallIcon: "ic_notification", /* iOS only properties */ //subtitle: "My Notification Subtitle", // (optional) smaller title below notification title }); } remoteNotification() { Dialog.showProgressDialog(); apiUpload.testNotification().then(res => { Dialog.dismissLoading(); toastShort("Success"); }).catch(err => { Dialog.dismissLoading(); toastShort(err); }) } copyToken() { Clipboard.setString(this.state.notifyToken); toastShort('已复制到剪贴板') } render() { return ( 注册的Token:{this.state.notifyToken}