Notify.js 4.8 KB

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