Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. View,
  7. SafeAreaView,
  8. Alert
  9. } from "react-native";
  10. import firebase from "react-native-firebase";
  11. import {
  12. Notification,
  13. NotificationOpen,
  14. RemoteMessage
  15. } from "react-native-firebase";
  16.  
  17. // Build a channel
  18. const channel = new firebase.notifications.Android.Channel(
  19. "test-channel",
  20. "Test Channel",
  21. firebase.notifications.Android.Importance.Max
  22. ).setDescription("WahedMobile test channel");
  23.  
  24. // Firebase check permissions
  25. firebase
  26. .messaging()
  27. .hasPermission()
  28. .then(enabled => {
  29. if (enabled) {
  30. // user has permissions
  31. } else {
  32. // user doesn't have permission
  33. // Ask user for permission
  34. firebase
  35. .messaging()
  36. .requestPermission()
  37. .then(() => {
  38. // User has authorised
  39. })
  40. .catch(error => {
  41. // User has rejected permissions
  42. });
  43. }
  44. });
  45.  
  46. interface Props {}
  47.  
  48. export class App extends Component {
  49. componentDidMount() {
  50. // Create the Android Channel
  51. firebase.notifications().android.createChannel(channel);
  52.  
  53. // Listen for Remotemessages
  54. this.messageListener = firebase
  55. .messaging()
  56. .onMessage((message: RemoteMessage) => {
  57. // Process your message as required
  58. console.log("Message Listener");
  59. });
  60. // Notification displayed
  61. this.notificationDisplayedListener = firebase
  62. .notifications()
  63. .onNotificationDisplayed((notification: Notification) => {
  64. // Process your notification as required
  65. // ANDROID: Remote notifications do not contain the channel ID.
  66. // You will have to specify this manually if you'd like to re-display the notification.
  67. console.log("Notification Displayed Listener");
  68. });
  69. /*
  70. * Triggered when a particular notification has been received in foreground
  71. * */
  72. this.notificationListener = firebase
  73. .notifications()
  74. .onNotification((notification: Notification) => {
  75. const { title, body } = notification;
  76. this.showAlert(title, body);
  77. console.log("Notification Received Listener");
  78. });
  79.  
  80. /*
  81. * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
  82. * */
  83. this.notificationOpenedListener = firebase
  84. .notifications()
  85. .onNotificationOpened((notificationOpen: NotificationOpen) => {
  86. // Get the action triggered by the notification being opened
  87. const action = notificationOpen.action;
  88. // Get information about the notification that was opened
  89. const notification: Notification = notificationOpen.notification;
  90. const { title, body } = notificationOpen.notification;
  91. // this.showAlert(title, body);
  92. console.log("Notification Opened Listener");
  93. });
  94.  
  95. // Check if app opened or closed
  96. firebase
  97. .notifications()
  98. .getInitialNotification()
  99. .then((notificationOpen: NotificationOpen) => {
  100. if (notificationOpen) {
  101. // App was opened by a notification
  102. // Get the action triggered by the notification being opened
  103. const action = notificationOpen.action;
  104. // Get information about the notification that was opened
  105. const notification: Notification = notificationOpen.notification;
  106. console.log("Initial Notification Listener");
  107. }
  108. });
  109. }
  110.  
  111. componentWillUnmount() {
  112. this.messageListener();
  113. this.notificationDisplayedListener();
  114. this.notificationListener();
  115. this.notificationOpenedListener();
  116. }
  117.  
  118. showAlert(title, body) {
  119. Alert.alert(
  120. title,
  121. body,
  122. [{ text: "OK", onPress: () => console.log("OK Pressed") }],
  123. { cancelable: false }
  124. );
  125. }
  126.  
  127. render() {
  128. return (
  129. <View style={styles.container}>
  130. <MainNavComp />
  131. </View>
  132. );
  133. }
  134. }
  135.  
  136. const styles = StyleSheet.create({
  137. container: {
  138. flex: 1,
  139. justifyContent: "center",
  140. alignItems: "center",
  141. backgroundColor: "#F5FCFF"
  142. }
  143. });
  144.  
  145. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement