-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
147 lines (126 loc) · 4.6 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, {Component} from 'react';
import { Platform, Alert, StyleSheet, Text, View, TextInput, TouchableOpacity, YellowBox } from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';
import firebase from 'react-native-firebase';
import appConfig from './app.json';
import PushController from './PushController.js'
YellowBox.ignoreWarnings(['Require cycle:', 'Remote debugger']);
const API_URL = "https://fcm.googleapis.com/fcm/send";
type Props = {};
type State = {
permissions: Object,
};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
senderId: appConfig.senderID,
fcmToken: "",
};
this.notif = new PushController(this.onRegister.bind(this), this.onNotification.bind(this));
}
async componentWillMount() {
const fcmToken = await firebase.messaging().getToken();
this.setState({ fcmToken })
PushNotificationIOS.addEventListener('notification', this._onRemoteNotification); // Use when app is in foreground for iOS
}
_onRemoteNotification(notification) {
if(Platform.OS === 'ios'){ // iOS won't display notifications if app is in the foreground.
PushNotification.localNotification({
alertAction: "view",
title: notification._data.title, // (optional)
message: notification._data.body, // (required)
});
}
}
onRegister(token) {
Alert.alert("Registered !", JSON.stringify(token));
console.log(token);
this.setState({ registerToken: token.token, gcmRegistered: true });
}
onNotification(notif) {
console.log(notif);
}
sendRemote(){
let body = {
"to" : this.state.fcmToken,
"notification" : {
"body" : "Say something!",
"title" : "Welcome to Push Notifications!",
"content_available" : true,
"priority" : "high",
},
"data" : {
"body" : "Say something!",
"title" : "Welcome to Push Notifications!",
"content_available" : true,
"priority" : "high",
}
}
this._send(JSON.stringify(body), "notification");
}
_send(body, type) {
let headers = new Headers({
"Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=<your-server-key>" // Put your server key here from the Firebase console.
});
fetch(API_URL, { method: "POST", headers, body })
.then(response => console.log("Send " + type + " response", response))
.catch(error => console.log("Error sending " + type, error));
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>React Native Push Notification</Text>
<View style={styles.spacer}></View>
<TextInput style={styles.textField} value={this.state.fcmToken} placeholder="FCM Token" />
<View style={styles.spacer}></View>
<TouchableOpacity style={styles.button} onPress={() => { this.notif.localNotif() }}><Text>Local Notification (now)</Text></TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => { this.sendRemote() }}><Text>Remote Notification</Text></TouchableOpacity>
<View style={styles.spacer}></View>
<TextInput style={styles.textField} value={this.state.senderId} onChangeText={(e) => {this.setState({ senderId: e })}} placeholder="Firebase Sender ID" />
<View style={styles.spacer}></View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
borderWidth: 1,
borderColor: "#000000",
margin: 5,
padding: 5,
width: "70%",
backgroundColor: "#DDDDDD",
borderRadius: 5,
},
textField: {
borderWidth: 1,
borderColor: "#AAAAAA",
margin: 5,
padding: 5,
width: "70%"
},
spacer: {
height: 10,
},
title: {
width: "100%",
fontWeight: "bold",
fontSize: 20,
textAlign: "center",
}
});