Notify.js 3.6 KB

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