Notify.js 5.1 KB

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