Commit 720709e4 authored by Libin Lu's avatar Libin Lu

update example

parent defb0962
import FirebaseConstants from "./FirebaseConstants";
import { Platform } from "react-native";
import { Platform, Alert } from "react-native";
const API_URL = "https://fcm.googleapis.com/fcm/send";
......@@ -17,12 +17,11 @@ class FirebaseClient {
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO"
"sound": "default"
},
"priority": 10
}
if(Platform.OS === 'android'){
body = {
"to": token,
......@@ -47,9 +46,7 @@ class FirebaseClient {
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO",
"remote": true
"sound": "default"
},
"priority": "normal"
}
......@@ -63,14 +60,10 @@ class FirebaseClient {
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
"sound": "default",
"click_action": "fcm.ACTION.HELLO"
"sound": "default"
},
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (DATA)",
"click_action": "fcm.ACTION.HELLO",
"remote": true
"hello": "there"
},
"priority": "high"
}
......@@ -78,16 +71,25 @@ class FirebaseClient {
this._send(JSON.stringify(body), "notification-data");
}
_send(body, type) {
async _send(body, type) {
if(FirebaseClient.KEY === 'YOUR_API_KEY'){
Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
return;
}
let headers = new Headers({
"Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=" + FirebaseConstants.KEY
});
fetch(API_URL, { method: "POST", headers, body })
.then(response => console.log("Send " + type + " response", response))
.catch(error => console.log("Error sending " + type, error));
try {
let response = await fetch(API_URL, { method: "POST", headers, body });
response = await response.json();
if(!response.success){
Alert.alert('Failed to send notification, check error log')
}
} catch (err) {
Alert.alert(err && err.message)
}
}
}
......
......@@ -422,6 +422,14 @@
name = Products;
sourceTree = "<group>";
};
3AF0A6071F7BE4DC004B899F /* Recovered References */ = {
isa = PBXGroup;
children = (
692E216422234A4CB6A7A838 /* libRNFIRMessaging.a */,
);
name = "Recovered References";
sourceTree = "<group>";
};
4339BFC21DAEBA3900F53B62 /* Products */ = {
isa = PBXGroup;
children = (
......@@ -497,6 +505,7 @@
00E356EF1AD99517003FC87E /* SimpleFcmClientTests */,
83CBBA001A601CBA00E9B192 /* Products */,
7E63D0154B6803F90ADF7C78 /* Pods */,
3AF0A6071F7BE4DC004B899F /* Recovered References */,
);
indentWidth = 2;
sourceTree = "<group>";
......
......@@ -45,7 +45,7 @@
[RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
......
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