Notify.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. export default class Notify extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. permission: false,
  13. notifyToken: 'none'
  14. };
  15. }
  16. componentDidMount() {
  17. //this.requestUserPermission()
  18. if (global.notifyToken.token) {
  19. this.setState({
  20. notifyToken: global.notifyToken.token
  21. })
  22. }
  23. }
  24. async requestUserPermission() {
  25. const authStatus = await messaging().requestPermission();
  26. const enabled =
  27. authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
  28. authStatus === messaging.AuthorizationStatus.PROVISIONAL;
  29. this.setState({
  30. permission: enabled
  31. })
  32. if (enabled) {
  33. console.log('Authorization status:', authStatus);
  34. }
  35. this.getToken()
  36. }
  37. getToken() {
  38. messaging().getToken().then(token => {
  39. if (token) {
  40. this.setState({
  41. notifyToken: token
  42. })
  43. console.log("token", token);
  44. }
  45. }).catch(err => {
  46. console.info('Token Error', err)
  47. });
  48. }
  49. localNotification() {
  50. PushNotification.localNotification({
  51. /* Android Only Properties */
  52. channelId: "10186", // (required) channelId, if the channel doesn't exist, notification will not trigger.
  53. showWhen: true, // (optional) default: true
  54. autoCancel: true, // (optional) default: true
  55. title: app.displayName, // (optional) default: "message" prop
  56. message: "This is a local test message", // (optional) default: none
  57. vibrate: true, // (optional) 振动开关default: true
  58. vibration: 300, // 振动长度(毫秒),如果vibrate=false,则忽略, default: 1000
  59. invokeApp: true, // 此选项允许单击操作将应用程序带回前台或留在后台, default: true
  60. smallIcon: "ic_notification",
  61. /* iOS only properties */
  62. //subtitle: "My Notification Subtitle", // (optional) smaller title below notification title
  63. });
  64. }
  65. remoteNotification() {
  66. Dialog.showProgressDialog();
  67. apiUpload.testNotification().then(res => {
  68. Dialog.dismissLoading();
  69. toastShort("Success");
  70. }).catch(err => {
  71. Dialog.dismissLoading();
  72. toastShort(err);
  73. })
  74. }
  75. copyToken() {
  76. Clipboard.setString(this.state.notifyToken);
  77. toastShort('已复制到剪贴板')
  78. }
  79. render() {
  80. return (
  81. <View style={styles.container}>
  82. <Text style={styles.text}>注册的Token:{this.state.notifyToken}</Text>
  83. <View style={styles.text}>
  84. <Button
  85. text='复制Token'
  86. elevation={2}
  87. onClick={() => this.copyToken()}
  88. />
  89. </View>
  90. <View style={styles.text}>
  91. <Button
  92. text='发送本地通知'
  93. elevation={2}
  94. onClick={() => this.localNotification()}
  95. />
  96. </View>
  97. <View style={styles.text}>
  98. <Button
  99. text='发送远程通知'
  100. elevation={2}
  101. onClick={() => this.remoteNotification()}
  102. />
  103. </View>
  104. <View style={[ui.flex1, ui.flexcc]}>
  105. <Text style={styles.os}>{Platform.OS}</Text>
  106. </View>
  107. </View>
  108. );
  109. }
  110. }
  111. const styles = StyleSheet.create({
  112. container: {
  113. flex: 1,
  114. ...$padding(8,16)
  115. },
  116. text: {
  117. paddingBottom: 12
  118. },
  119. os: {
  120. color: '#999'
  121. }
  122. })