Commit 98939636 authored by Libin Lu's avatar Libin Lu

move functions

parent d84c9712
...@@ -65,6 +65,69 @@ export default class App extends Component { ...@@ -65,6 +65,69 @@ export default class App extends Component {
}); });
} }
sendRemoteNotification(token) {
let body;
if(Platform.OS === 'android'){
body = {
"to": token,
"data":{
"custom_notification": {
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default",
"priority": "high",
"show_in_foreground": true
}
},
"priority": 10
}
} else {
body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default"
},
"priority": 10
}
}
firebaseClient.send(JSON.stringify(body), "notification");
}
sendRemoteData(token) {
let body = {
"to": token,
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default"
},
"priority": "normal"
}
firebaseClient.send(JSON.stringify(body), "data");
}
sendRemoteNotificationWithData(token) {
let body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
"sound": "default"
},
"data":{
"hello": "there"
},
"priority": "high"
}
firebaseClient.send(JSON.stringify(body), "notification-data");
}
render() { render() {
let { token, tokenCopyFeedback } = this.state; let { token, tokenCopyFeedback } = this.state;
...@@ -94,16 +157,16 @@ export default class App extends Component { ...@@ -94,16 +157,16 @@ export default class App extends Component {
Remote notif won't be available to iOS emulators Remote notif won't be available to iOS emulators
</Text> </Text>
<TouchableOpacity onPress={() => firebaseClient.sendNotification(token)} style={styles.button}> <TouchableOpacity onPress={() => firebaseClient.sendRemoteNotification(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Notification</Text> <Text style={styles.buttonText}>Send Remote Notification</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity onPress={() => firebaseClient.sendData(token)} style={styles.button}> <TouchableOpacity onPress={() => firebaseClient.sendRemoteData(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Data</Text> <Text style={styles.buttonText}>Send Remote Data</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity onPress={() => firebaseClient.sendNotificationWithData(token)} style={styles.button}> <TouchableOpacity onPress={() => firebaseClient.sendRemoteNotificationWithData(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Notification With Data</Text> <Text style={styles.buttonText}>Send Remote Notification With Data</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity onPress={() => this.showLocalNotification()} style={styles.button}> <TouchableOpacity onPress={() => this.showLocalNotification()} style={styles.button}>
......
...@@ -11,70 +11,7 @@ class FirebaseClient { ...@@ -11,70 +11,7 @@ class FirebaseClient {
this.sendNotificationWithData = this.sendNotificationWithData.bind(this); this.sendNotificationWithData = this.sendNotificationWithData.bind(this);
} }
sendNotification(token) { async send(body, type) {
let body;
if(Platform.OS === 'android'){
body = {
"to": token,
"data":{
"custom_notification": {
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default",
"priority": "high",
"show_in_foreground": true
}
},
"priority": 10
}
} else {
body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default"
},
"priority": 10
}
}
this._send(JSON.stringify(body), "notification");
}
sendData(token) {
let body = {
"to": token,
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default"
},
"priority": "normal"
}
this._send(JSON.stringify(body), "data");
}
sendNotificationWithData(token) {
let body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
"sound": "default"
},
"data":{
"hello": "there"
},
"priority": "high"
}
this._send(JSON.stringify(body), "notification-data");
}
async _send(body, type) {
if(FirebaseClient.KEY === 'YOUR_API_KEY'){ if(FirebaseClient.KEY === 'YOUR_API_KEY'){
Alert.alert('Set your API_KEY in app/FirebaseConstants.js') Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment