Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Platform, StyleSheet, Text, View, AppState } from "react-native";
  3. import { Root } from "native-base";
  4. import { Provider } from "react-redux";
  5. import { PersistGate } from "redux-persist/es/integration/react";
  6. import axios from 'axios';
  7. import PushNotification from 'react-native-push-notification';
  8.  
  9. import url from './config/api_service';
  10. import AppNavigation from "./navigations";
  11. import configureStore from "./store/store";
  12. import Axios from "axios";
  13. import VirtualizedList from "./pages/components/VirtualizedList";
  14.  
  15. const { store, persistor } = configureStore();
  16.  
  17. export default class App extends Component {
  18. constructor() {
  19. super();
  20. this.state = {
  21. isReady: false,
  22. platformSystem:'',
  23. versionApps:'',
  24. appState: AppState.currentState,
  25. notificationOpen: false,
  26. status: ''
  27. };
  28. this.configureNotif();
  29. this.onCheck();
  30. this._handleAppStateChange = this._handleAppStateChange.bind(this)
  31. this.interval;
  32. }
  33.  
  34.  
  35. componentDidMount() {
  36. AppState.addEventListener('change', this._handleAppStateChange);
  37. }
  38.  
  39. componentWillUnmount() {
  40. AppState.removeEventListener('change', this._handleAppStateChange);
  41. }
  42.  
  43. configureNotif(){
  44. PushNotification.configure({
  45.  
  46. // (optional) Called when Token is generated (iOS and Android)
  47. onRegister: function(token) {
  48. console.log( 'TOKEN:', token );
  49. },
  50.  
  51. // (required) Called when a remote or local notification is opened or received
  52. onNotification: (notification) => {
  53. const status = notification.message.split('-')[0]
  54. this.setState({
  55. notificationOpen: true,
  56. status
  57. })
  58.  
  59. // process the notification
  60.  
  61. // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
  62. },
  63.  
  64. // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
  65.  
  66. // IOS ONLY (optional): default: all - Permissions to register.
  67. permissions: {
  68. alert: true,
  69. badge: true,
  70. sound: true
  71. },
  72.  
  73. // Should the initial notification be popped automatically
  74. // default: true
  75. popInitialNotification: true,
  76.  
  77. /**
  78. * (optional) default: true
  79. * - Specified if permissions (ios) and token (android and ios) will requested or not,
  80. * - if not, you must call PushNotificationsHandler.requestPermissions() later
  81. */
  82. requestPermissions: true,
  83. });
  84. }
  85.  
  86. _handleAppStateChange = (nextAppState) => {
  87. // if(nextAppState === 'background'){
  88. // console.log(nextAppState)
  89. // this.interval = setInterval(this.onCheck, 720000)
  90. // }
  91. }
  92.  
  93. async onCheck(){
  94. try {
  95. const notif = await Axios.get('http://elma.myadais.com/api/ebis_getnotification')
  96. await notif.data.forEach((element, index) => {
  97. console.warn('called')
  98. PushNotification.localNotification({
  99. /* Android Only Properties */
  100. id: index, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
  101. ticker: "My Notification Ticker", // (optional)
  102. autoCancel: true, // (optional) default: true
  103. largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
  104. smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
  105. // bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
  106. subText: "A new update", // (optional) default: none
  107. color: "red", // (optional) default: system default
  108. vibrate: true, // (optional) default: true
  109. vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
  110. tag: 'some_tag', // (optional) add tag to message
  111. // group: "group", // (optional) add group to message
  112. ongoing: false, // (optional) set whether this is an "ongoing" notification
  113. priority: "high", // (optional) set notification priority, default: high
  114. visibility: "public", // (optional) set notification visibility, default: private
  115. importance: "high", // (optional) set notification importance, default: high
  116. /* iOS and Android properties */
  117. title: "EPM Mobile", // (optional)
  118. message: `${element.NOTIF_STATUS}-${element.TOTAL}`, // (required)
  119. playSound: false, // (optional) default: true
  120. soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
  121. number: '10', // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
  122. repeatType: 'day', // (optional) Repeating interval. Check 'Repeating Notifications' section for more info.
  123. // actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
  124. });
  125. });
  126. } catch (err) {
  127. console.warn(err)
  128. }
  129. }
  130.  
  131. render() {
  132. if(this.state.notificationOpen){
  133. return (
  134. <VirtualizedList
  135. status={this.state.status}
  136. onBack={() => this.setState({ notificationOpen: false })}
  137. />
  138. )
  139. }
  140. return (
  141. <Provider store={store}>
  142. <PersistGate loading={<View style={{justifyContent:'center', alignItems:'center', flex:1}}><Text style={{textAlign:'center'}}>Loading...</Text></View>} persistor={persistor}>
  143. <Root>
  144. <AppNavigation />
  145. </Root>
  146. </PersistGate>
  147. </Provider>
  148. );
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement