Guest User

App.js

a guest
May 22nd, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6.  
  7. import React, { Component } from 'react';
  8. import {
  9. Platform,
  10. StyleSheet,
  11. Text,
  12. View,
  13. TouchableOpacity
  14. } from 'react-native';
  15.  
  16. import firebase from 'react-native-firebase';
  17. import type, { RemoteMessage, Notification, NotificationOpen } from 'react-native-firebase';
  18.  
  19.  
  20. const instructions = Platform.select({
  21. ios: 'Press Cmd+R to reload,\n' +
  22. 'Cmd+D or shake for dev menu',
  23. android: 'Double tap R on your keyboard to reload,\n' +
  24. 'Shake or press menu button for dev menu',
  25. });
  26.  
  27. const notification = new firebase.notifications.Notification()
  28. .setNotificationId('notificationId')
  29. .setTitle('My notification title')
  30. .setBody('My notification body')
  31. .setData({
  32. key1: 'value1',
  33. key2: 'value2',
  34. });
  35.  
  36.  
  37. // // Build a channel
  38. // const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
  39. // .setDescription('My apps test channel');
  40.  
  41. // // Create the channel
  42. // firebase.notifications().android.createChannel(channel);
  43.  
  44. type Props = {};
  45. export default class App extends Component<Props> {
  46.  
  47. componentDidMount() {
  48.  
  49. firebase.messaging().hasPermission()
  50. .then(enabled => {
  51. if (enabled) {
  52. // user has permissions
  53. this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
  54. // Process your notification as required
  55. // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
  56. onNotification = (notification: Notification) => {
  57. notification.android.setChannelId(notification.data.channelId);
  58. firebase.notifications().displayNotification(notification);
  59. };
  60. });
  61. this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
  62. // Process your notification as required
  63. });
  64.  
  65. this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
  66. // Get the action triggered by the notification being opened
  67. const action = notificationOpen.action;
  68. // Get information about the notification that was opened
  69. const notification: Notification = notificationOpen.notification;
  70. });
  71.  
  72. firebase.notifications().getInitialNotification()
  73. .then((notificationOpen: NotificationOpen) => {
  74. if (notificationOpen) {
  75. // App was opened by a notification
  76. // Get the action triggered by the notification being opened
  77. const action = notificationOpen.action;
  78. // Get information about the notification that was opened
  79. const notification: Notification = notificationOpen.notification;
  80. }
  81. });
  82.  
  83. } else {
  84. // user doesn't have permission
  85. firebase.messaging().requestPermission()
  86. .then(() => {
  87. // User has authorised
  88. this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
  89. // Process your notification as required
  90. // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
  91. });
  92. this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
  93. // Process your notification as required
  94. });
  95. })
  96. .catch(error => {
  97. // User has rejected permissions
  98. });
  99. }
  100. });
  101. }
  102.  
  103. componentWillUnmount() {
  104. this.notificationDisplayedListener();
  105. this.notificationListener();
  106. this.notificationOpenedListener();
  107. }
  108.  
  109. render() {
  110. return (
  111. <View style={styles.container}>
  112. <Text style={styles.welcome}>
  113. Welcome to React Native!
  114. </Text>
  115. <TouchableOpacity
  116. style={{backgroundColor: 'cyan'}}
  117. onPress={()=>{
  118. firebase.notifications().displayNotification(notification);
  119. // firebase.auth()
  120. // .signInAnonymouslyAndRetrieveData()
  121. // .then(credential => {
  122. // if (credential) {
  123. // console.log('default app user ->', credential.user.toJSON());
  124. // }
  125. // });
  126. }}>
  127. <Text>Test</Text>
  128. </TouchableOpacity>
  129. <Text style={styles.instructions}>
  130. To get started, edit App.js
  131. </Text>
  132. <Text style={styles.instructions}>
  133. {instructions}
  134. </Text>
  135. </View>
  136. );
  137. }
  138. }
  139.  
  140. const styles = StyleSheet.create({
  141. container: {
  142. flex: 1,
  143. justifyContent: 'center',
  144. alignItems: 'center',
  145. backgroundColor: '#F5FCFF',
  146. },
  147. welcome: {
  148. fontSize: 20,
  149. textAlign: 'center',
  150. margin: 10,
  151. },
  152. instructions: {
  153. textAlign: 'center',
  154. color: '#333333',
  155. marginBottom: 5,
  156. },
  157. });
Advertisement
Add Comment
Please, Sign In to add comment