Notify.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React, { Component } from 'react';
  2. import { View, Text, StyleSheet, Platform } from 'react-native';
  3. import Clipboard from '@react-native-clipboard/clipboard';
  4. import Button from '../../components/Button';
  5. import PushNotification from "react-native-push-notification";
  6. import Dialog from '../../components/Dialog';
  7. import apiUpload from '../../api/apiUpload';
  8. import app from '../../../app.json'
  9. import utils from '../../utils/utils';
  10. import Skeleton from '../../components/VbeSkeleton';
  11. import crashlytics from '@react-native-firebase/crashlytics';
  12. export default class Notify extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. permission: false,
  17. notifyToken: '',
  18. serverToken: ''
  19. };
  20. }
  21. componentDidMount() {
  22. //this.requestUserPermission()
  23. if (global.notifyToken.token) {
  24. this.setState({
  25. notifyToken: global.notifyToken.token
  26. })
  27. }
  28. this.getUserToken();
  29. }
  30. getUserToken() {
  31. getUserInfo(info => {
  32. if (info.firebaseToken) {
  33. let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
  34. this.setState({
  35. serverToken: token ?? ""
  36. });
  37. }
  38. }, true);
  39. }
  40. async requestUserPermission() {
  41. const authStatus = await messaging().requestPermission();
  42. const enabled =
  43. authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
  44. authStatus === messaging.AuthorizationStatus.PROVISIONAL;
  45. this.setState({
  46. permission: enabled
  47. })
  48. if (enabled) {
  49. console.log('Authorization status:', authStatus);
  50. }
  51. this.getToken()
  52. }
  53. getToken() {
  54. messaging().getToken().then(token => {
  55. if (token) {
  56. this.setState({
  57. notifyToken: token
  58. })
  59. console.log("token", token);
  60. }
  61. }).catch(err => {
  62. console.info('Token Error', err)
  63. });
  64. }
  65. localNotification() {
  66. PushNotification.localNotification({
  67. /* Android Only Properties */
  68. channelId: "10186", // (required) channelId, if the channel doesn't exist, notification will not trigger.
  69. showWhen: true, // (optional) default: true
  70. autoCancel: true, // (optional) default: true
  71. title: app.displayName, // (optional) default: "message" prop
  72. message: "This is a local test message", // (optional) default: none
  73. vibrate: true, // (optional) 振动开关default: true
  74. vibration: 300, // 振动长度(毫秒),如果vibrate=false,则忽略, default: 1000
  75. invokeApp: true, // 此选项允许单击操作将应用程序带回前台或留在后台, default: true
  76. smallIcon: "ic_notification",
  77. /* iOS only properties */
  78. //subtitle: "My Notification Subtitle", // (optional) smaller title below notification title
  79. });
  80. }
  81. remoteNotification() {
  82. Dialog.showProgressDialog();
  83. apiUpload.testNotification().then(res => {
  84. Dialog.dismissLoading();
  85. toastShort("Success");
  86. }).catch(err => {
  87. Dialog.dismissLoading();
  88. toastShort(err);
  89. })
  90. }
  91. updateToken() {
  92. if (getUserId() > 0) {
  93. Dialog.showProgressDialog();
  94. utils.registerFirebaseToken("", res => {
  95. Dialog.dismissLoading();
  96. toastShort(res ? "Upload success" : "Upload failed")
  97. if (res) {
  98. this.getUserToken();
  99. }
  100. })
  101. } else {
  102. toastShort("Please login first");
  103. }
  104. }
  105. copyToken() {
  106. if (this.state.notifyToken) {
  107. Clipboard.setString(this.state.notifyToken);
  108. toastShort('Copied')
  109. }
  110. }
  111. async testCrashlytics() {
  112. console.log("测试触发崩溃");
  113. await crashlytics().setCrashlyticsCollectionEnabled(true);
  114. crashlytics().log("Test Crashlytics...");
  115. crashlytics().crash();
  116. }
  117. render() {
  118. return (
  119. <View style={styles.container}>
  120. <Text style={styles.text}>Device Token:{this.state.notifyToken || "none"}</Text>
  121. <Text style={styles.text}>Server Token:{this.state.serverToken || "none"}</Text>
  122. { utils.isNotEmpty(this.state.notifyToken) &&
  123. <View style={styles.text}>
  124. <Button
  125. text='Copy Token'
  126. elevation={2}
  127. onClick={() => this.copyToken()}
  128. />
  129. </View>
  130. }
  131. <View style={styles.text}>
  132. <Button
  133. text='Send Local Notification'
  134. elevation={2}
  135. onClick={() => this.localNotification()}
  136. />
  137. </View>
  138. <View style={styles.text}>
  139. <Button
  140. text='Request Remote Notification'
  141. elevation={2}
  142. onClick={() => this.remoteNotification()}
  143. />
  144. </View>
  145. { utils.isNotEmpty(this.state.notifyToken) &&
  146. <View style={styles.text}>
  147. <Button
  148. text='Update Firebase Token'
  149. elevation={2}
  150. onClick={() => this.updateToken()}
  151. />
  152. </View>
  153. }
  154. <View style={styles.text}>
  155. <Button
  156. text='Test Crashlytics'
  157. elevation={2}
  158. onClick={() => this.testCrashlytics()}
  159. />
  160. </View>
  161. <View style={[ui.flex1, ui.flexcc]}>
  162. <Text style={styles.os}>{Platform.OS}</Text>
  163. </View>
  164. <Skeleton
  165. containerStyle={ui.flex1}
  166. isLoading={true}
  167. boneColor='#eeeeee'
  168. highlightColor='#f6f6f6'
  169. animationType="shiver"
  170. layout={[{width: '60%', height: 20, marginLeft: 30, borderRadius: 3}]}
  171. animationDirection={'horizontalRight'}/>
  172. </View>
  173. );
  174. }
  175. }
  176. const styles = StyleSheet.create({
  177. container: {
  178. flex: 1,
  179. ...$padding(8,16)
  180. },
  181. text: {
  182. paddingBottom: 12
  183. },
  184. os: {
  185. color: '#999'
  186. }
  187. })