|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { Text, View, TouchableOpacity, AsyncStorage } from "react-native"; |
| 3 | +import { Notifications, Permissions } from "expo"; |
| 4 | + |
| 5 | +export default class Notification extends Component { |
| 6 | + registerForPushNotification = async () => { |
| 7 | + const enabled = await this.pushNotificationsEnabled(); |
| 8 | + |
| 9 | + if (!enabled) { |
| 10 | + return Promise.resolve(); |
| 11 | + } |
| 12 | + |
| 13 | + return Notifications.getExpoPushTokenAsync(); |
| 14 | + }; |
| 15 | + |
| 16 | + pushNotificationsEnabled = async () => { |
| 17 | + const { status: existingStatus } = await Permissions.getAsync( |
| 18 | + Permissions.NOTIFICATIONS |
| 19 | + ); |
| 20 | + let finalStatus = existingStatus; |
| 21 | + if (existingStatus !== "granted") { |
| 22 | + const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); |
| 23 | + finalStatus = status; |
| 24 | + } |
| 25 | + if (finalStatus !== "granted") { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + return true; |
| 30 | + }; |
| 31 | + |
| 32 | + handleNotificationStatus = async () => { |
| 33 | + let token = await this.registerForPushNotification(); |
| 34 | + console.log(token); |
| 35 | + if (token) { |
| 36 | + await AsyncStorage.setItem("notificationStatus", "true"); |
| 37 | + } |
| 38 | + this.goToHome(); |
| 39 | + }; |
| 40 | + |
| 41 | + goToHome = () => { |
| 42 | + this.props.navigation.navigate("HomeNavigator"); |
| 43 | + }; |
| 44 | + |
| 45 | + render() { |
| 46 | + return ( |
| 47 | + <View |
| 48 | + style={{ alignItems: "stretch", justifyContent: "center", flex: 1 }} |
| 49 | + > |
| 50 | + <Text style={{ fontSize: 48, margin: 30, textAlign: "center" }}> |
| 51 | + 🙏 |
| 52 | + </Text> |
| 53 | + <Text |
| 54 | + style={{ |
| 55 | + color: "#fff", |
| 56 | + fontSize: 36, |
| 57 | + textAlign: "center", |
| 58 | + margin: 30 |
| 59 | + }} |
| 60 | + > |
| 61 | + Namaste Detective Pikachu |
| 62 | + </Text> |
| 63 | + <Text |
| 64 | + style={{ |
| 65 | + color: "#fff", |
| 66 | + fontSize: 30, |
| 67 | + textAlign: "center", |
| 68 | + margin: 30 |
| 69 | + }} |
| 70 | + > |
| 71 | + Do you want me to notify 🔔 about new complaints? |
| 72 | + </Text> |
| 73 | + <TouchableOpacity |
| 74 | + style={{ |
| 75 | + backgroundColor: "#00FFFF", |
| 76 | + height: 60, |
| 77 | + margin: 15, |
| 78 | + alignItems: "center", |
| 79 | + justifyContent: "center" |
| 80 | + }} |
| 81 | + onPress={() => this.handleNotificationStatus()} |
| 82 | + > |
| 83 | + <Text style={{ fontSize: 36 }}>Yes Pleeezz 😀</Text> |
| 84 | + </TouchableOpacity> |
| 85 | + <TouchableOpacity |
| 86 | + style={{ |
| 87 | + backgroundColor: "#00FFFF", |
| 88 | + height: 60, |
| 89 | + margin: 15, |
| 90 | + alignItems: "center", |
| 91 | + justifyContent: "center" |
| 92 | + }} |
| 93 | + onPress={() => this.goToHome()} |
| 94 | + > |
| 95 | + <Text style={{ fontSize: 36 }}>No 😭</Text> |
| 96 | + </TouchableOpacity> |
| 97 | + </View> |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments